Autosize UIPickerView Component UILabels in textField inputView

Hello,


I have successfully implemented a UIPickerView as the inputView of my custom textField. Because its a hand made UIPickerView, I add 3 labels next to each component to indicate (hour, minute, second)


Now it seem the inputView is autosized based on wdith and device orientation and my UIPickerView adapts to it. This is fine and all but the UILabels mentioned above, do not autosize. I tried various methods but can't seem to adjust it dependent on width or rotation.


Heres my code (within the UITextField subclass)


var picker:UIPickerView = UIPickerView()


override func awakeFromNib() {

super.awakeFromNib()

picker.delegate = self

picker.dataSource = self

(pickerHours,pickerMinutes,pickerSeconds) = MakeTimeFromString(self.text!)

self.inputView = configurePicker()

self.inputAccessoryView = configureAccessoryView()

}


func configurePicker() -> UIView {

picker.backgroundColor = UIColor.clearColor()

let hoursLabel: UILabel = UILabel(frame: CGRectMake((picker.frame.size.width / 3) - 30, 95, 75, 30))

hoursLabel.text = "hour"

hoursLabel.textColor = UIColor.whiteColor()

picker.addSubview(hoursLabel)

let minutesLabel: UILabel = UILabel(frame: CGRectMake((picker.frame.size.width / 2) + 25, 95 , 75, 30))

minutesLabel.text = "min"

minutesLabel.textColor = UIColor.whiteColor()

picker.addSubview(minutesLabel)

let secondsLabel: UILabel = UILabel(frame: CGRectMake(((picker.frame.size.width / 3) * 2) + 75, 95, 75, 30))

secondsLabel.text = "sec"

secondsLabel.textColor = UIColor.whiteColor()

picker.addSubview(secondsLabel)

picker.selectRow(pickerHours, inComponent: 0, animated: true)

picker.selectRow(pickerMinutes, inComponent: 1, animated: true)

picker.selectRow(pickerSeconds, inComponent: 2, animated: true)

return picker

}

Accepted Reply

Nevermind, ended up subclassing UIPickerView and doing it myself.


For anyone interested. This will adapt in any view.


class CustomUIPickerView : UIPickerView {
   
    var hoursLabel: UILabel!
    var minutesLabel: UILabel!
    var secondsLabel: UILabel!
   
    override init(frame: CGRect) {
        super.init(frame: frame)
       
        hoursLabel = UILabel(frame: CGRectMake(((self.frame.size.width / 3) / 2) + 25, (self.frame.size.height / 2) -  15, 50, 30))
        minutesLabel = UILabel(frame: CGRectMake((self.frame.size.width / 2) + 25, (self.frame.size.height / 2) -  15 , 50, 30))
        secondsLabel = UILabel(frame: CGRectMake(((self.frame.size.width ) * (5 / 6)) + 25, (self.frame.size.height / 2) -  15, 50, 30))
        hoursLabel.text = "hour"
        hoursLabel.textColor = UIColor.whiteColor()
       
        minutesLabel.text = "min"
        minutesLabel.textColor = UIColor.whiteColor()
   
        secondsLabel.text = "sec"
        secondsLabel.textColor = UIColor.whiteColor()
       
        self.addSubview(secondsLabel)
        self.addSubview(minutesLabel)
        self.addSubview(hoursLabel)
    }
   
    override func layoutSubviews() {
       
        super.layoutSubviews()
       
        hoursLabel.frame = CGRectMake(((self.frame.size.width / 3) / 2) + 25, (self.frame.size.height / 2) -  15, 50, 30)
       
        minutesLabel.frame = CGRectMake((self.frame.size.width / 2) + 25, (self.frame.size.height / 2) -  15 , 50, 30)
       
        secondsLabel.frame = CGRectMake(((self.frame.size.width ) * (5 / 6)) + 25, (self.frame.size.height / 2) -  15, 50, 30)
    }
   
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }
   
}

Replies

Nevermind, ended up subclassing UIPickerView and doing it myself.


For anyone interested. This will adapt in any view.


class CustomUIPickerView : UIPickerView {
   
    var hoursLabel: UILabel!
    var minutesLabel: UILabel!
    var secondsLabel: UILabel!
   
    override init(frame: CGRect) {
        super.init(frame: frame)
       
        hoursLabel = UILabel(frame: CGRectMake(((self.frame.size.width / 3) / 2) + 25, (self.frame.size.height / 2) -  15, 50, 30))
        minutesLabel = UILabel(frame: CGRectMake((self.frame.size.width / 2) + 25, (self.frame.size.height / 2) -  15 , 50, 30))
        secondsLabel = UILabel(frame: CGRectMake(((self.frame.size.width ) * (5 / 6)) + 25, (self.frame.size.height / 2) -  15, 50, 30))
        hoursLabel.text = "hour"
        hoursLabel.textColor = UIColor.whiteColor()
       
        minutesLabel.text = "min"
        minutesLabel.textColor = UIColor.whiteColor()
   
        secondsLabel.text = "sec"
        secondsLabel.textColor = UIColor.whiteColor()
       
        self.addSubview(secondsLabel)
        self.addSubview(minutesLabel)
        self.addSubview(hoursLabel)
    }
   
    override func layoutSubviews() {
       
        super.layoutSubviews()
       
        hoursLabel.frame = CGRectMake(((self.frame.size.width / 3) / 2) + 25, (self.frame.size.height / 2) -  15, 50, 30)
       
        minutesLabel.frame = CGRectMake((self.frame.size.width / 2) + 25, (self.frame.size.height / 2) -  15 , 50, 30)
       
        secondsLabel.frame = CGRectMake(((self.frame.size.width ) * (5 / 6)) + 25, (self.frame.size.height / 2) -  15, 50, 30)
    }
   
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }
   
}