Swift - 用于UIPickerView的带有完成按钮的UIToolbar在横向上消失

时间:2018-06-07 16:30:43

标签: ios swift uipickerview uitoolbar

我将UIPickerView作为UITextField的输入,并使用"完成"实现UIToolbar。按钮。它完美无缺,但当我在iPhone上切换到横向模式时,UIToolbar消失了。这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    thePicker.delegate = self

    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default

    toolBar.isTranslucent = true
    toolBar.tintColor = UIColor(red: 76/255, green: 100/255, blue: 217/255, alpha: 1)
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Aceptar", style: UIBarButtonItemStyle.plain, target: self, action: #selector(doneClick))


    toolBar.setItems([doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true

    testTextField.inputView = thePicker
    testTextField.inputAccessoryView = toolBar

}

1 个答案:

答案 0 :(得分:0)

当我以编程方式添加UIPickerView并为PickerView添加UIToolBar时,遇到了相同的问题。只需为UIPickerView添加[.flexibleWidth,.flexibleHeight]

self.thePicker.autoresizingMask = [.flexibleWidth,.flexibleHeight]