Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

ios - How can I change background Color of Stepper in SwiftUI?

I want to the change the background color for stepper but this code change the background color for text too. I want to change only the stepper's background color. please guide me to solve this issue.

    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"))
}
question from:https://stackoverflow.com/questions/66061289/how-can-i-change-background-color-of-stepper-in-swiftui

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should give UIColor to Color;

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

You need to unwrap UIColor(named: "Color1") as it is an optional value. You can do it in different ways according to what you need. One solution might be like;

    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))
        
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...