使用UIPickerView时出现约束错误。

问题描述 投票:0回答:1

每当我点击一个我创建的pickerviews时,我都会收到以下错误记录到控制台。

[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
    }

(顺便说一下,我试过在picker和工具栏上打开和关闭 translatesAutoResizingMaskIntoConstraints,但错误仍然发生)

swift datepicker uipickerview
1个回答
0
投票

该约束错误是由 UIToolBar. 这似乎是当前操作系统的一个问题。 要关闭警告,请初始化 UIToolbar 指定宽度为100,它仍然会延伸到边框。

let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 100.0, height: 44.0))

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

© www.soinside.com 2019 - 2024. All rights reserved.