所需的菜单选择器文本颜色未正确显示
我有两个视图:createexerciseview和repsviewStyle。尽管CreateExerciseView具有其他视图元素,但我将凝结代码以演示我的特定问题。看来,选择器没有显示我想要的所需颜色,而是根本没有出现,或者在某些情况下以默认的蓝色文本出现。
这是凝结的createexerciseview代码:
struct CreateExerciseView: View {
// Reps/Sets Properties
@State var reps: String = "1 rep"
@State var sets: String = "1 set"
var body: some View {
Group {
// MARK: REPS AND SETS
RepsViewStyle(reps: $reps, sets: $sets)
}
.padding(.horizontal)
}
}
这是repsviewStyle代码:
struct RepsViewStyle: View {
@Binding var reps: String
@Binding var sets: String
var body: some View {
VStack {
HStack {
Text("Select repetitions and sets: ")
.fontWeight(.semibold)
.padding([.top, .trailing])
Spacer()
}
Divider()
HStack {
Spacer()
Picker(selection: $reps,
label: Text("\(reps)"),
content: {
ForEach(1..<100) { number in
if number == 1 {
Text("\(number) rep")
.tag("\(number) rep")
} else {
Text("\(number) reps")
.tag("\(number) reps")
}
}
})
.pickerStyle(MenuPickerStyle())
.padding(12)
.padding(.horizontal, 20)
.background(Color.MyTheme.redColor)
.cornerRadius(10)
.shadow(color: Color.MyTheme.greyColor.opacity(0.3), radius: 10, x: 0, y: 10)
.foregroundColor(Color.white)
.font(.headline)
Picker(selection: $sets,
label: Text("\(sets)"),
content: {
ForEach(1..<20) { number in
if number == 1 {
Text("\(number) set")
.tag("\(number) set")
} else {
Text("\(number) sets")
.tag("\(number) sets")
}
}
})
.pickerStyle(MenuPickerStyle())
.padding(12)
.padding(.horizontal, 20)
.background(Color.MyTheme.redColor)
.cornerRadius(10)
.shadow(color: Color.MyTheme.greyColor.opacity(0.3), radius: 10, x: 0, y: 10)
.foregroundColor(Color.white)
.font(.headline)
Spacer()
}
.padding()
}
}
}
我尝试将属性应用于repsviewStyle的调用,从初始视图以及挑选器本身,但它们本身,但它们本身,但它们似乎根本没有改变。
I have two views: CreateExerciseView and RepsViewStyle. Although CreateExerciseView has other view elements, I will condense the code to demonstrate my specific issue. It seems that the picker is not displaying with the desired colour I want, instead not appearing at all or in some cases, appearing as default blue text.
Here is the condensed CreateExerciseView code:
struct CreateExerciseView: View {
// Reps/Sets Properties
@State var reps: String = "1 rep"
@State var sets: String = "1 set"
var body: some View {
Group {
// MARK: REPS AND SETS
RepsViewStyle(reps: $reps, sets: $sets)
}
.padding(.horizontal)
}
}
And here is the RepsViewStyle code:
struct RepsViewStyle: View {
@Binding var reps: String
@Binding var sets: String
var body: some View {
VStack {
HStack {
Text("Select repetitions and sets: ")
.fontWeight(.semibold)
.padding([.top, .trailing])
Spacer()
}
Divider()
HStack {
Spacer()
Picker(selection: $reps,
label: Text("\(reps)"),
content: {
ForEach(1..<100) { number in
if number == 1 {
Text("\(number) rep")
.tag("\(number) rep")
} else {
Text("\(number) reps")
.tag("\(number) reps")
}
}
})
.pickerStyle(MenuPickerStyle())
.padding(12)
.padding(.horizontal, 20)
.background(Color.MyTheme.redColor)
.cornerRadius(10)
.shadow(color: Color.MyTheme.greyColor.opacity(0.3), radius: 10, x: 0, y: 10)
.foregroundColor(Color.white)
.font(.headline)
Picker(selection: $sets,
label: Text("\(sets)"),
content: {
ForEach(1..<20) { number in
if number == 1 {
Text("\(number) set")
.tag("\(number) set")
} else {
Text("\(number) sets")
.tag("\(number) sets")
}
}
})
.pickerStyle(MenuPickerStyle())
.padding(12)
.padding(.horizontal, 20)
.background(Color.MyTheme.redColor)
.cornerRadius(10)
.shadow(color: Color.MyTheme.greyColor.opacity(0.3), radius: 10, x: 0, y: 10)
.foregroundColor(Color.white)
.font(.headline)
Spacer()
}
.padding()
}
}
}
I have tried applying attributes to both the calling of the RepsViewStyle from the initial view as well as the pickers themselves but they do not seem to change at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
.AccentColor(.white)
添加到选择器,替换.foregroundColor(color.white)
以设置“标签”颜色白色,而不是默认的蓝色,如果您想获得的话。
You can add
.accentColor(.white)
to the Pickers, replacing.foregroundColor(Color.white)
to set the "label" color white,rather than the default blue color, if that is what you wanted to acheive.
如前所述,很难在纯SwiftUi中样式
picker
,因为自定义的拾音器尚未(尚未公开)。您的情况的解决方法可能是使用
菜单
。这项工作的造型。As mentioned it is difficult to style
Picker
in pure SwiftUI, as custom PickerStyles are not (yet) public.A workaround for your case might be using
Menu
instead. The styling on this one works.