[英]SwiftUI SegmentedPickerStyle width proportional to Text length
我正在尝试构建一个宽度与文本长度成比例的分段控件。 我当前的代码:
import SwiftUI
struct ContentView: View {
@State private var selectedElement = ""
var body: some View {
VStack {
HStack {
Text("Some choice").foregroundColor(Color.black)
Spacer()
Picker("aaa", selection: $selectedElement, content: {
Text("choice 1").foregroundColor(Color.black)
Text("another choice").foregroundColor(Color.black)
})
}.pickerStyle(SegmentedPickerStyle()).padding(16)
Spacer()
}.background(Color.white)
}
}
构建此代码导致:代码结果
我想看到的是:红色矩形显示每个部分应该占用多少空间的预期结果。 我怎样才能做到这一点? 另外,为什么.foregroundColor(Color.black)
对分段控件中的文本颜色没有影响?
解决方案很简单,在选择器后添加 .fixedSize() :
Picker("aaa", selection: $selectedElement, content: {
Text("choice 1").foregroundColor(Color.black)
Text("another choice").foregroundColor(Color.black)
}).fixedSize()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.