Eyzuky Eyzuky - 14 days ago 123
Swift Question

UIPickerView is cut in the middle on iPad simulator only

I have a

UIView
with height = 1.0, and a picker view that is attached on it with Autolayout (it appears below the view).

My picker view's width should be equal as the line seperator (the UIView).

On iPhone it works great, but on iPad the picker view is sliced in the middle.
I tested in the "Debug view hierarchy", and saw that it is actually in the width of the view, but still cut in the middle (see screenshot added).

I then thought that it may be a delegate function issue, so I added these implementations:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let rowInfo = getRowText(for: pickerView, fromRow: row, andComponent: component)

let field = (view as? UITextField) ?? UITextField()
field.font = font16
field.textAlignment = .left
field.textColor = .black
field.text = rowInfo.text
return field
}
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return self.aSeperator.frame.width
}


Screenshots:
On simulator

on debug view hierarchy

enter image description here

this are the autolayout constraints I implemented for that picker view
enter image description here

Answer Source

Found the problem! The issue was that I was calling this function on the UIPickerView from viewDidLoad:

    clientPickerView.roundCorners([.bottomLeft, .bottomRight], radius: 4.0)
    clientSitePicker.roundCorners([.bottomLeft, .bottomRight], radius: 4.0)

This function makes changes to the mask of the picker view adding round corners to it (as seen in the images above):

func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
    let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
    let mask = CAShapeLayer()
    mask.path = path.cgPath
    self.layer.mask = mask
}

The solution was to move this call to viewDidLayoutSubviews.