使用 UIPickerView 时出现约束错误

fis*_*sch 3 datepicker uipickerview swift

每当我单击我创建的选择器视图之一时,控制台都会记录以下错误:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
Run Code Online (Sandbox Code Playgroud)

我知道当存在冲突的约束时通常会发生此错误,但我不知道在这种情况下冲突的约束是什么。pickerview 仍然可以正常工作,但我仍然想摆脱这个错误。

代码:

@IBAction func showDatePicker(_ sender: PickerField){
        let datePicker = UIDatePicker
        let datePickerField = sender



        datePicker.translatesAutoresizingMaskIntoConstraints = false
        datePicker.datePickerMode = .time

        let toolbar = UIToolbar()
        toolbar.translatesAutoresizingMaskIntoConstraints = false
        toolbar.sizeToFit()
        let finishedButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donedatePicker))
        finishedButton.tag = sender.tag
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelDatePicker))
        cancelButton.tag = sender.tag

        toolbar.setItems([cancelButton,spaceButton,finishedButton], animated: false)

        datePickerField.inputView = datePicker
        datePickerField.inputAccessoryView = toolbar
    }
Run Code Online (Sandbox Code Playgroud)

(仅供参考,我尝试打开和关闭选择器和工具栏的translatesAutoResizingMaskIntoConstraints,但错误仍然发生)

rba*_*win 5

约束误差是由 引起的UIToolBar。这似乎是当前操作系统的问题。要消除警告,请将UIToolbar指定宽度初始化为 100,它仍然会拉伸到边框。

let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 100.0, height: 44.0))
Run Code Online (Sandbox Code Playgroud)

你不需要使用.sizeToFit(),也不需要.translatesAutoresizingMaskIntoConstraints = false