通过 UiToolBar 的 UiPickerView - Swift 4 升级错误

UiPickerView over UiToolBar - Swift 4 Upgrade Error

我将项目更新为 Swift 4 XCode 9 我有一个 InputText 和一个使用 Picker 的输入视图 我在 InputText 添加了一个 UIToolBar。 它在升级之前运行良好。 现在就像 UIPickerView 它超过了 UIToolBar.

我能看到,但如果我点击它就像我点击 UIPickerView

UIToolBar 它工作得很好,因为如果我将工具栏设置为普通的 TextInput,它会显示并且工作正常..

可以是什么?

这是我的代码:

var picker: UIPickerView
picker = UIPickerView(frame: CGRect(x: 0, y: 200, width: view.frame.width, height: 300))
picker.backgroundColor = .white

picker.showsSelectionIndicator = true
picker.delegate = self
picker.dataSource = PickerData as? UIPickerViewDataSource


textField.inputView = picker

let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = .blue
toolBar.sizeToFit()

let doneButton = UIBarButtonItem(title: "Seleziona", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.doneFunction(_:)))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Annulla", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.doneFunction(_:)))

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

textField.inputAccessoryView = toolBar

我也尝试了 picker.addSubview(toolBar),但结果相同。

我有同样的问题,我用背景视图解决了它,并在其上添加了 2 个子视图 - 选择器和工具栏。 这是代码:

    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.tintColor = green
    toolBar.sizeToFit()

    picker = UIPickerView(frame: CGRect(x: 0, y: toolBar.frame.size.height, width: view.frame.width, height: 300))
    picker.tintColor = green
    picker.showsSelectionIndicator = true
    picker.delegate = self
    picker.dataSource = self

    let bgView = UIView(frame: CGRect(x: 0, y: 200, width: view.frame.width, height: 300 + toolBar.frame.size.height))

    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(nextPicker))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelPicker))
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true

    bgView.addSubview(picker)
    bgView.addSubview(toolBar)
    cell.textField.inputView = bgView

希望对您有所帮助。