SwiftUI-如何调整PickerView的大小?

时间:2020-02-05 15:07:53

标签: swiftui picker

如何在SwiftUI中调整选择器视图的大小?我需要更改占用的宽度。我下面的代码只是一个简单的视图,其中包含一个选择器。更改width参数不会更改选择器视图的宽度。

struct CalibrationBar: View {
@State var tone = Int()
var body: some View{
    HStack{

        Button(action: {
                playTone(tone: self.tone, amp: 50, stop: true)
            }) {
                Text("50 dB")
        }
        .frame(width: 60.0)

            Picker(selection: .constant(1), label: Text("")) {
            Text("+0").tag(1)
            Text("+2").tag(2)
            Text("+4").tag(3)
            }
            .clipped()
            .frame(minWidth: 0, maxWidth: 100)
            .labelsHidden()

     }
   }
}

2 个答案:

答案 0 :(得分:8)

struct ContentView: View {
var body: some View{
    HStack{

        Button(action: {
            }) {
                Text("50 dB")
        }
        .frame(width: 60.0)

        VStack {
            Picker(selection: .constant(1), label: Text("")) {
            Text("+0").tag(1)
            Text("+2").tag(2)
            Text("+4").tag(3)
            }
            //.clipped()
            .frame(width: 50)
            .clipped()
        }.border(Color.red)
     }
   }
}

enter image description here

答案 1 :(得分:-1)

您可以通过使用变换效果来更改其大小,此示例会将Picker的大小减小为默认大小的0.8

Picker(selection: self.$choice, label: Text("Pick One")) {
                ForEach(0 ..< self.choices.count) {
                    Text(self.choices[$0])
                }
            }.transformEffect(.init(scaleX: 0.8, y: 0.8))