简体   繁体   中英

How to Give title to the component in UIPickerview iOS swift

i want to know that is it possible to give title to each component like i have given in pic here?... Portrait mode

i have set the labels by adding label as subview, but i also have to manage it in landscape mode so here it is... landscape mode

SO, My question is is possible to get the selected label's frame from particular component? i mean i can manage it without adding subviews so i can manage this label in all devices and in all orientation.

thanks !!!

The key is to set the custom title for selected row.
As per selectedRow function in UIPickerView, you are returned with the selected row in the component. So you don't need to worry about adding an extra label for that or handle orientation modes.
Try doing this ( title is what you are currently displaying in each row):

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch component {
        case 0:
            if self.yourPickerView.selectedRow(inComponent: 0) == row
            {
                return "\(title) Hour"
            }
            else
            {
                return title
            }
        case 1:
            if self.yourPickerView.selectedRow(inComponent: 1) == row
            {
                return "\(title) Min"
            }
            else
            {
                return title
            }
        case 2:
            if self.yourPickerView.selectedRow(inComponent: 2) == row
            {
                return "\(title) Sec"
            }
            else
            {
                return title
            }
        default:

            return ""
        }
    }  

Then finally in didSelectRow :

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
    // your code
    pickerView.reloadAllComponents()
}  

Though this won't be the best way (I guess), but it is certainly better than placing a separate label and handling orientations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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