【发布时间】:2020-05-23 02:49:22
【问题描述】:
每当我点击我创建的其中一个选择器视图时,我都会在控制台中记录以下错误:
[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)
我知道当存在冲突的约束时通常会发生此错误,但我不知道在这种情况下冲突的约束是什么。 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
}
(仅供参考,我已尝试为选择器和工具栏打开和关闭 translatesAutoResizingMaskIntoConstraints,但错误仍然存在)
【问题讨论】:
标签: swift datepicker uipickerview