仅在 iPad 模拟器上 UIPickerView 在中间被截断

UIPickerView is cut in the middle on iPad simulator only

我有一个高度 = 1.0 的 UIView,以及一个使用自动布局附加在其上的选择器视图(它显示在视图下方)。

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

在 iPhone 上效果很好,但在 iPad 上,选择器视图在中间被切开。 我在"Debug view hierarchy"里测试了一下,发现其实是在view的宽度上,但还是在中间切了(见截图补充)。

后来我想可能是委托函数的问题,所以我添加了这些实现:

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
}

截图:

这是我为该选择器视图实施的自动布局约束

找到问题了! 问题是我在 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