简体   繁体   English

iOS上的UIPickerView

[英]UIPickerView on iOS

I was working on this UIPickleView on iOS and I am struggling with how can I customize it like changing its font, font sizes, stroke color, and space between them. 我在iOS上使用此UIPickleView进行工作时,我在如何自定义它(如更改其字体,字体大小,笔触颜色以及它们之间的间距)方面感到困惑。 Here is what I got so far: 这是到目前为止我得到的:

import UIKit

class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
    var picker_arr :[String]!
    var com1_arr :[String]!
    var com2_arr :[String]!

    @IBOutlet weak var picker: UIPickerView!

    override func viewDidLoad() {
        super.viewDidLoad()
        picker_arr = ["12:45", "13:00","13:15","13:30","13:45"]
        com2_arr = ["today","tomorrow"]
        com1_arr = ["1 person", "2 people","3 people","4 people","5 people"]
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if(component==0){
            return com1_arr.count
        }
        if(component==1){
            return com2_arr.count
        }
        return picker_arr.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if(component==0){
            return com1_arr[row]
        }
        if(component==1){
            return com2_arr[row]
        }
        return picker_arr[row]
    }
}

If you read up on UIPickerViewDelegate you can see that it says 如果您在UIPickerViewDelegate上阅读,您会看到它说

The methods in this group are marked @optional. 该组中的方法标记为@optional。 However, to use a picker view, you must implement either the pickerView( :titleForRow:forComponent:) or the pickerView( :viewForRow:forComponent:reusing:) method to provide the content of component rows. 但是,要使用选择器视图,必须实现pickerView( :titleForRow:forComponent :)或pickerView( :viewForRow: forComponent :reusing :)方法来提供组件行的内容。

You have chosen to use titleForRow , but you should probably use viewForRow instead. 您已经选择使用titleForRow ,但是您可能应该使用viewForRow That way, instead of returning a String , you can return an entire UILabel and give it your text, font, color etc. Read the usage for viewForRow here . 这样,您可以返回整个UILabel并为其提供文本,字体,颜色等,而不是返回String在此处阅读viewForRow的用法。

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

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