在 Swift 上更改选择器视图的颜色

Posted

技术标签:

【中文标题】在 Swift 上更改选择器视图的颜色【英文标题】:Changing colour of picker view on Swift 【发布时间】:2017-12-09 11:57:30 【问题描述】:

我的 viewController 上有 5 个选择器视图。我想更改所有选择器视图的颜色。我做了一切,但我无法成功。它默认给我黑色。我想让它变白。我怎样才能解决这个问题。谁能帮我解决我的代码?另外,用户从pickerview输入一些数据后,如果他或她先返回页面然后再次来到这个屏幕,数据将保留他或她输入到pickerview的任何内容,此过程后数据将不会被清除。我该怎么做?

class LoggedInVC: UIViewController, UIPickerViewDataSource,UIPickerViewDelegate

    @IBOutlet weak var cinsiyetButton: UIButton!
    @IBOutlet weak var türButton: UIButton!
    @IBOutlet weak var ageButton: UIButton!
    @IBOutlet weak var colorBotton: UIButton!
    @IBOutlet weak var healthButton: UIButton!
    @IBOutlet weak var cinsiyetPicker: UIPickerView!
    @IBOutlet weak var turPicher: UIPickerView!
    @IBOutlet weak var agePicker: UIPickerView!
    @IBOutlet weak var colorPicker: UIPickerView!
    @IBOutlet weak var healthPicker: UIPickerView!

    let cinsiyet = ["Erkek","Dişi"]
    let tur = ["pug","terrier","dogo","doberman"]
    let color = ["siyah","beyaz","kahverangi","karışık"]
    let age = ["1","2","3","4","5"]
    let health = ["kısır","Kısır Değil"]

    override func viewDidLoad() 
        super.viewDidLoad()

        cinsiyetPicker.dataSource = self
        cinsiyetPicker.delegate = self
        turPicher.dataSource = self
        turPicher.delegate = self
        agePicker.dataSource = self
        agePicker.delegate = self
        colorPicker.dataSource = self
        colorPicker.delegate = self
        healthPicker.dataSource = self
        healthPicker.delegate = self
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
    

    @IBAction func cinsiyetPressed(_ sender: Any) 
        cinsiyetPicker.isHidden = false
    

    @IBAction func turPressed(_ sender: Any) 
        turPicher.isHidden = false

    

    @IBAction func agePressed(_ sender: Any) 

        agePicker.isHidden = false
    

    @IBAction func colorPressed(_ sender: Any) 

        colorPicker.isHidden = false

    

    @IBAction func healthPressed(_ sender: Any) 
        healthPicker.isHidden = false
    

    func numberOfComponents(in pickerView: UIPickerView) -> Int 
        return 1
    

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        if pickerView.tag==1
            return cinsiyet.count
        
        else if pickerView.tag==2
            return tur.count
        
        else if pickerView.tag==3
            return age.count
        
        else if pickerView.tag==4
            return color.count
        
        else if pickerView.tag==5 
            return health.count
        
        return health.count
    

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 

        if pickerView.tag==1
            return cinsiyet[row]
        
        else if pickerView.tag==2
            return tur[row]
        
        else if pickerView.tag==3
            return age[row]
        
        else if pickerView.tag==4
            return color[row]
        
        else if pickerView.tag==5 
            return health[row]
        
        return health[row]
    

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 

        if pickerView.tag==1
            cinsiyetButton.setTitle(cinsiyet[row], for: UIControlState())
            cinsiyetPicker.isHidden = true
        
        if pickerView.tag==2
            türButton.setTitle(tur[row], for: UIControlState())
            turPicher.isHidden = true
        
        if pickerView.tag==3
            ageButton.setTitle(age[row], for: UIControlState())
            agePicker.isHidden = true
        
        if pickerView.tag==4
            colorBotton.setTitle(color[row], for: UIControlState())
            colorPicker.isHidden = true
        
        if pickerView.tag==5
            healthButton.setTitle(health[row], for: UIControlState())
            healthPicker.isHidden = true
        
        

【问题讨论】:

你的问题不清楚。您实际上要更改选择器的哪一部分?到目前为止,您尝试过什么? 【参考方案1】:

我认为只需使用此委托方法即可实现您的要求

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?

【讨论】:

你能详细输入吗? 通过改变前景色或背景色返回任何属性字符串,例如:NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName: UIColor.black])

以上是关于在 Swift 上更改选择器视图的颜色的主要内容,如果未能解决你的问题,请参考以下文章

使用颜色选择器更改片段中edittext的背景颜色

如何快速更改选择器视图的文本颜色? [复制]

如何更改 UIPickerView 选择器的颜色

更改日期选择器的文本颜色

如何在 iPhone 中更改 UIPickerView 的背景颜色

更改时间选择器的文本颜色 [android]