简体   繁体   English

更改段 Controller Tint Color in SwiftUI

[英]Change Segment Controller Tint Color in SwiftUI

I am using a segment picker in my app.我在我的应用程序中使用了细分选择器 I want to change the text color of the unselected to white .我想将未选中的文本颜色更改为白色 I searched for the solutions but didn't get any right for me.我搜索了解决方案,但没有找到适合我的解决方案。 Here is my code:这是我的代码:

struct QuestionView: View {

@State private var selectedIndex = 0
    
    init() {
        
        UISegmentedControl.appearance().backgroundColor = UIColor(named: ColorName.appBlue.rawValue)
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor(named: ColorName.appBlue.rawValue)!], for: .selected)
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.green], for: .normal)
        UISegmentedControl.appearance().setTitleTextAttributes([
            .font : UIFont.preferredFont(forTextStyle: .headline)
        ], for: .normal)
    }

var body: some View {

 VStack(alignment: .leading) {
            
            NavBar(navTitle: "Questionnaire")
            
            HStack {
                
                Text("Is this a live claim?")
                    .font(.custom(InterFont.bold.rawValue, size: 24))
                
                Spacer()
                
                Picker("What is your favorite color?", selection: $selectedIndex) {
                    Text("Yes").tag(1)
                         
                    Text("No").tag(0)
                    
                }
                .pickerStyle(.segmented)
                .frame(width: 130)
                .cornerRadius(40)
                
            }.padding()
            Spacer()
        }
    }
}

From this code,I am getting this:从这段代码,我得到这个:

我得到了什么

The tint color changes for the selected one but not for the unselected.所选颜色的色调颜色会更改,但未选中的颜色不会更改。 I want white color for the unselected one.我想要未选中的白色。 Here is what I required:这是我需要的:

必需的

The same thing happened to me due to the font property.由于字体属性,同样的事情发生在我身上。 Please try to remove the last statement that is used to set the font of the Segmentation Controller.请尝试删除最后一条用于设置 Segmentation Controller 字体的语句。

init() {
        
        UISegmentedControl.appearance().backgroundColor = UIColor(named: ColorName.appBlue.rawValue)
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor(named: ColorName.appBlue.rawValue)!], for: .selected)
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.green], for: .normal)

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM