iOS UIPickerView自定义行视图宽度错误

时间:2018-06-28 09:36:45

标签: ios iphone swift uipickerview custom-view

为以下方法返回自定义视图以自定义选择器行:

public override func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    return PickerAccessoryView.instantiateFromNib()
}

并返回完整宽度:

public func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
    return pickerView.frame.width
}

不调整行视图的大小:O

1 个答案:

答案 0 :(得分:2)

通过将自定义视图的初始帧设置为.zero来解决:

public override func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let view = PickerAccessoryView.instantiateFromNib()
    view.frame = .zero
    return view
}