如何在带有其他组件的 ScrollView 或 VStack 中完全显示 Swiftui 中的选择器?

Cel*_*ina 6 watchos swiftui

一旦我将 Picker 放入 ScrollView 或 VStack 并且其中有更多代码(如 ForEach 循环),则仅显示 Picker-label 。

在此处输入图片说明 我想知道为什么,因为在 Scrollview 之外或单独它显示正确。

这是代码:

@State private var exercises = ["Unterarmstütz", "Dehnen", "Kopfstand", "Handstand"]
@State private var selectedExercise = "Plank"
@State private var selectedTimeIndex = 60

var body: some View {
    VStack {
       ScrollView {
            Picker(selection: $selectedTimeIndex, label: Text("select Time")) {
                Text("Option 1")
                Text("Option 2")
                Text("Option 3")
            }
            ForEach(exercises.identified(by: \.self)) { exercise in
                Button(action: {
                    self.selectedExercise = String("exercise")
                }) {
                    Text("exercise")
                }
            }

        }
    }

}
Run Code Online (Sandbox Code Playgroud)

Ket*_*dra 5

问题是ScrollView,如果你在里面放置任何内容,ScrollView那么你必须设置它的框架。

在这里你可以做到,

@State private var exercises = ["Unterarmstütz", "Dehnen", "Kopfstand", "Handstand"]
@State private var selectedExercise = "Plank"
@State private var selectedTimeIndex = 60

var body: some View {

       //use GeometryReader for height & weight//

        GeometryReader { geometry in       

            ScrollView() {

                VStack {

                    Picker(selection: self.$selectedTimeIndex, label: Text("select Time")) {
                        Text("Option 1")
                        Text("Option 2")
                        Text("Option 3")
                    }.frame(width: geometry.size.width, height: geometry.size.height, alignment: .center)

                    ForEach(self.exercises, id: \.self) { exercise in
                        Button(action: {
                            self.selectedExercise = String("exercise")
                        }) {
                            Text("exercise")
                        }
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

注意:我使用的是 Xcode 11 Beta 4