所需的菜单选择器文本颜色未正确显示

发布于 2025-01-28 19:17:49 字数 3530 浏览 3 评论 0原文

我有两个视图: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.

The CreateExerciseView which incorrectly displays the picker colour

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最偏执的依靠 2025-02-04 19:17:49

您可以将.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.

七秒鱼° 2025-02-04 19:17:49

如前所述,很难在纯SwiftUi中样式picker,因为自定义的拾音器尚未(尚未公开)。

您的情况的解决方法可能是使用菜单。这项工作的造型。

                Menu {
                    ForEach(1..<100) { number in
                        Button { reps = "\(number) reps" } label: {
                            if number == 1 {
                                Text("\(number) rep")
                            } else {
                                Text("\(number) reps")
                            }
                        }
                    }
                } label: {
                    Text("\(reps)")
                        .foregroundStyle(.white)
                        .font(.headline)
                        .padding(12)
                        .padding(.horizontal, 20)
                        .background(Color.red)
                        .cornerRadius(10)
                        .shadow(color: Color.gray.opacity(0.3), radius: 10, x: 0, y: 10)
                }

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.

                Menu {
                    ForEach(1..<100) { number in
                        Button { reps = "\(number) reps" } label: {
                            if number == 1 {
                                Text("\(number) rep")
                            } else {
                                Text("\(number) reps")
                            }
                        }
                    }
                } label: {
                    Text("\(reps)")
                        .foregroundStyle(.white)
                        .font(.headline)
                        .padding(12)
                        .padding(.horizontal, 20)
                        .background(Color.red)
                        .cornerRadius(10)
                        .shadow(color: Color.gray.opacity(0.3), radius: 10, x: 0, y: 10)
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文