ios - 如何在 SwiftUI 中更改步进器的背景颜色?

标签 ios swift swiftui

我想改变步进器的背景颜色,但这段代码也改变了文本的背景颜色。我只想更改步进器的背景颜色。请指导我解决这个问题。

    HStack {

    Text("Count : ")
        .font(.body)
        .foregroundColor(Color(.blue))

    Spacer()

    Stepper(value: self.$MyViewModel.MyModel.minCounts, in: 5...60) {

     Text("\(String(format: "%.0f",MyViewModel.MyModel.minCounts)) Mins")
         .font(.body)
         .foregroundColor(Color("font"))

    }
    .background(Color("Color1"))
}

最佳答案

你应该给 Color 赋予 UIColor;

Stepper(value: $age, in: 1...60)
        {
            Text("\(String(format: "%.0f",44.456)) Mins")
                .font(.body)
                .foregroundColor(Color("font"))
        }
        .background(Color(UIColor(named: "Color1")!))

您需要解包 UIColor(named: "Color1"),因为它是一个可选值。您可以根据需要以不同的方式进行。一种解决方案可能是这样的;

    let color1: UIColor = UIColor(named: "Color1") ?? .white
    var body: some View {
        HStack{
                Text("Count : ")
                    .font(.body)
                    .foregroundColor(Color(.blue))
        Spacer()
        Stepper(value: $age, in: 1...60)
        {
            Text("\(String(format: "%.0f",44.456)) Mins")
                .font(.body)
                .foregroundColor(Color("font"))
        }
        .background(Color(color1))
        
    }
}

关于ios - 如何在 SwiftUI 中更改步进器的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66061289/

相关文章:

objective-c - Fading UIView 允许 subview 可见

ios - UILocalNotification 不会在 didEnterRegion 中触发

c# - Xamarin Http 请求仅适用于 iOS 模拟器

SwiftUI - 展开 TextField 以填充 View

ios - 如果不推荐使用 NetService,我应该如何在 macOS 上执行 mDNS?

ios - 为什么我在 Swift5 中收不到推送消息?

ios - 单击更改多个 UIButton 的背景颜色

iphone - 解包可选值时出错 AND dequeueReusableCellWithIdentifier( :cell identifier)

ios - 使用ForEach循环和Picker的SwiftUI意外索引

swift - 为什么我不能在调用 func 时改变最初设置为某个参数的变量?