Liya
Liya

Reputation: 1

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. 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]
    }
}

Upvotes: 0

Views: 57

Answers (1)

Sti
Sti

Reputation: 8503

If you read up on UIPickerViewDelegate you can see that it says

The methods in this group are marked @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.

You have chosen to use titleForRow, but you should probably use viewForRow instead. 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.

Upvotes: 2

Related Questions