UIPickerView仅在iPad模拟器的中间切割

时间:2017-09-28 15:04:13

标签: swift uipickerview

我有一个UIView,身高= 1.0,并附有一个使用Autolayout附加的选择器视图(它显示在视图下方)。

我的选择器视图的宽度应该与行分隔符(UIView)相等。

在iPhone上它运行良好,但在iPad上,选择器视图在中间切成薄片。 我在" Debug视图层次结构"中进行了测试,发现它实际上在视图的宽度上,但仍然在中间切割(参见截图)。

然后我认为这可能是委托功能问题,所以我添加了这些实现:

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
}

截图: On simulator

on debug view hierarchy

enter image description here

这是我为该选择器视图实现的自动布局约束 enter image description here

1 个答案:

答案 0 :(得分:1)

发现问题了! 问题是我在UIPickerView上的viewDidLoad调用此函数:

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

此功能会更改选择器视图的遮罩,为其添加圆角(如上图所示):

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
}

解决方案是将此调用移至viewDidLayoutSubviews