【问题标题】:Swift texfield input: pickerView, throws constraint errorSwift 文本字段输入:uipickerView,引发约束错误
【发布时间】:2021-12-14 14:08:18
【问题描述】:

当我单击五个文本字段时,会出现选择器视图,但出现此错误。当您单击下方的两个文本字段时,选择器视图不会出现。如果我在滚动视图中制作这整个内容,则下部文本字段也可以与选择器视图一起使用。我找不到任何解决这个问题的方法 qwq 视图控制器中的所有东西都是自动调整大小的。

故事板图片:https://imgur.com/a/9gYud1D

代码:

import UIKit
import Charts
import CoreData

class ExamViewController: TabView, ChartViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
    
    @IBOutlet weak var block1Label: UILabel!
    @IBOutlet weak var block2Label: UILabel!
    
    @IBOutlet weak var P1Field: UITextField!
    @IBOutlet weak var P1Points: UITextField!
    
    @IBOutlet weak var P2Field: UITextField!
    @IBOutlet weak var P2Points: UITextField!
    
    @IBOutlet weak var P3Field: UITextField!
    @IBOutlet weak var P3Points: UITextField!
    
    @IBOutlet weak var P4Field: UITextField!
    @IBOutlet weak var P4Points: UITextField!
    
    @IBOutlet weak var P5Field: UITextField!
    @IBOutlet weak var P5Points: UITextField!
    

    @IBOutlet weak var scrollView: UIScrollView!

    
    var strCurrentFieldEditing: String = ""
    var picks: [String] = []
    var numberPicks: [String] = [ "0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"]
    let subjectPicker = UIPickerView()
    let numberPicker = UIPickerView()
    let toolBar = UIToolbar()
    
    let notificationAlert = UIAlertController(title: "qwq", message: "qwq#2", preferredStyle: .alert)
    
    override func viewDidLoad() {
        super.viewDidLoad();
        update()
        
        subjectPicker.delegate = self
        numberPicker.delegate = self
        
        P1Field.delegate = self
        P2Field.delegate = self
        P3Field.delegate = self
        P4Field.delegate = self
        P5Field.delegate = self
        
        P1Field.inputView = subjectPicker
        P1Field.inputAccessoryView = toolBar
        
        P2Field.inputView = subjectPicker
        P2Field.inputAccessoryView = toolBar
        
        P3Field.inputView = subjectPicker
        P3Field.inputAccessoryView = toolBar
        
        P4Field.inputView = subjectPicker
        P4Field.inputAccessoryView = toolBar
        
        P5Field.inputView = subjectPicker
        P5Field.inputAccessoryView = toolBar
        
        self.navigationItem.title = "Prüfungsübersicht"
        
        notificationAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
        
        scrollView.contentSize = CGSize(
            width: scrollView.visibleSize.width,
            height: scrollView.visibleSize.height*1.1
        )
    }
    
    func pickUp(_ textField : UITextField){
        toolBar.barStyle = .default
        toolBar.isTranslucent = true
        toolBar.tintColor = UIColor.accentColor
       toolBar.sizeToFit()
       
        
        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ExamViewController.donePicker))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ExamViewController.cancelPicker))
        toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
        toolBar.isUserInteractionEnabled = true
     }
    
    @objc func donePicker() {
        if(strCurrentFieldEditing == "P1"){
            P1Field.resignFirstResponder()
        }else if(strCurrentFieldEditing == "P2"){
            P2Field.resignFirstResponder()
        }else if(strCurrentFieldEditing == "P3"){
            P3Field.resignFirstResponder()
        }else if(strCurrentFieldEditing == "P4"){
            P4Field.resignFirstResponder()
        }else if(strCurrentFieldEditing == "P5"){
            P5Field.resignFirstResponder()
        }
       update()
    }
    
    @objc func cancelPicker(){
        if(strCurrentFieldEditing == "P1"){
            P1Field.resignFirstResponder()
            P1Points.text = nil
        }else if(strCurrentFieldEditing == "P2"){
            P2Field.resignFirstResponder()
            P2Points.text = nil
        }else if(strCurrentFieldEditing == "P3"){
            P3Field.resignFirstResponder()
            P3Points.text = nil
        }else if(strCurrentFieldEditing == "P4"){
            P4Field.resignFirstResponder()
            P4Points.text = nil
        }else if(strCurrentFieldEditing == "P5"){
            P5Field.resignFirstResponder()
            P5Points.text = nil
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        update()
    }
    
    
    // MARK: UIPickerView Delegation
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if(component == 0){
            return picks.count
        }else {
            return numberPicks.count
        }
    }
    
    func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if(component == 0){
            return  picks[row]
        }else {
            return  numberPicks[row]
        }
    }
    
    func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if(picks.count == 0) {
            self.present(self.notificationAlert, animated: true, completion: nil)
            return }
        if(component == 0){
            if(strCurrentFieldEditing == "P1"){
                P1Field.text = picks[row]
            }else if(strCurrentFieldEditing == "P2"){
                P2Field.text = picks[row]
            }else if(strCurrentFieldEditing == "P3"){
                P3Field.text = picks[row]
            }else if(strCurrentFieldEditing == "P4"){
                P4Field.text = picks[row]
            }else if(strCurrentFieldEditing == "P5"){
                P5Field.text = picks[row]
            }
        }else {
            if(strCurrentFieldEditing == "P1"){
                P1Points.text = numberPicks[row]
            }else if(strCurrentFieldEditing == "P2"){
                P2Points.text = numberPicks[row]
            }else if(strCurrentFieldEditing == "P3"){
                P3Points.text = numberPicks[row]
            }else if(strCurrentFieldEditing == "P4"){
                P4Points.text = numberPicks[row]
            }else if(strCurrentFieldEditing == "P5"){
                P5Points.text = numberPicks[row]
            }
        }
    }
    

    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if(picks.count == 0){ return false}
        if(textField == P1Field){
            self.strCurrentFieldEditing = "P1"
        }else if(textField == P2Field){
            self.strCurrentFieldEditing = "P2"
        }else if(textField == P3Field){
            self.strCurrentFieldEditing = "P3"
        }else if(textField == P4Field){
            self.strCurrentFieldEditing = "P4"
        }else if(textField == P5Field){
            self.strCurrentFieldEditing = "P5"
        }
        self.pickUp(textField)
        return true
    }
    
    
    func update() {
        picks = Util.getAllSubjectNames()
        if((picks.firstIndex(of: P1Field.text ?? "")) != nil){
            picks.remove(at: picks.firstIndex(of: P1Field.text ?? "")!)
        }
        if((picks.firstIndex(of: P2Field.text ?? "")) != nil){
            picks.remove(at: picks.firstIndex(of: P2Field.text ?? "")!)
        }
        if((picks.firstIndex(of: P3Field.text ?? "")) != nil){
            picks.remove(at: picks.firstIndex(of: P3Field.text ?? "")!)
        }
        if((picks.firstIndex(of: P4Field.text ?? "")) != nil){
            picks.remove(at: picks.firstIndex(of: P4Field.text ?? "")!)
        }
        if((picks.firstIndex(of: P5Field.text ?? "")) != nil){
            picks.remove(at: picks.firstIndex(of: P5Field.text ?? "")!)
        }
       
        }
        
    }
    
}

这是我的错误

2021-10-29 15:16:32.284195+0200 Calq[3986:101986] Writing analzed variants.
objc[3986]: Class _PathPoint is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x118606a78) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x136e958b0). One of the two will be used. Which one is undefined.
objc[3986]: Class _PointQueue is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x118606a50) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x136e958d8). One of the two will be used. Which one is undefined.
2021-10-29 15:16:32.594144+0200 Calq[3986:101986] [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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x6000012f0960 h=--& v=--& _UIToolbarContentView:0x7f8620b429e0.width == 0   (active)>",
    "<NSLayoutConstraint:0x6000012e0730 H:|-(0)-[_UIButtonBarStackView:0x7f8620b43290]   (active, names: '|':_UIToolbarContentView:0x7f8620b429e0 )>",
    "<NSLayoutConstraint:0x6000012e0780 H:[_UIButtonBarStackView:0x7f8620b43290]-(0)-|   (active, names: '|':_UIToolbarContentView:0x7f8620b429e0 )>",
    "<NSLayoutConstraint:0x6000012fb6b0 'TB_Leading_Leading' H:|-(0)-[_UIModernBarButton:0x7f861ff7c3b0'Cancel']   (active, names: '|':_UIButtonBarButton:0x7f861ff7be90 )>",
    "<NSLayoutConstraint:0x6000012fb700 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x7f861ff7c3b0'Cancel']-(8)-|   (active, names: '|':_UIButtonBarButton:0x7f861ff7be90 )>",
    "<NSLayoutConstraint:0x6000012f0000 'UISV-canvas-connection' UILayoutGuide:0x6000008a3020'UIViewLayoutMarginsGuide'.leading == _UIButtonBarButton:0x7f861ff7be90.leading   (active)>",
    "<NSLayoutConstraint:0x6000012f0230 'UISV-canvas-connection' UILayoutGuide:0x6000008a3020'UIViewLayoutMarginsGuide'.trailing == _UIButtonBarButton:0x7f861ff7eb70.trailing   (active)>",
    "<NSLayoutConstraint:0x6000012f0280 'UISV-spacing' H:[_UIButtonBarButton:0x7f861ff7be90]-(0)-[UIView:0x7f861ff7ea00]   (active)>",
    "<NSLayoutConstraint:0x6000012f02d0 'UISV-spacing' H:[UIView:0x7f861ff7ea00]-(0)-[_UIButtonBarButton:0x7f861ff7eb70]   (active)>",
    "<NSLayoutConstraint:0x6000012e0550 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x6000008a3020'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIButtonBarStackView:0x7f8620b43290 )>",
    "<NSLayoutConstraint:0x6000012e05f0 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x6000008a3020'UIViewLayoutMarginsGuide']-(0)-|(LTR)   (active, names: '|':_UIButtonBarStackView:0x7f8620b43290 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000012fb700 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x7f861ff7c3b0'Cancel']-(8)-|   (active, names: '|':_UIButtonBarButton:0x7f861ff7be90 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2021-10-29 15:16:32.595554+0200 Calq[3986:101986] [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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x6000012f0960 h=--& v=--& _UIToolbarContentView:0x7f8620b429e0.width == 0   (active)>",
    "<NSLayoutConstraint:0x6000012e0730 H:|-(0)-[_UIButtonBarStackView:0x7f8620b43290]   (active, names: '|':_UIToolbarContentView:0x7f8620b429e0 )>",
    "<NSLayoutConstraint:0x6000012e0780 H:[_UIButtonBarStackView:0x7f8620b43290]-(0)-|   (active, names: '|':_UIToolbarContentView:0x7f8620b429e0 )>",
    "<NSLayoutConstraint:0x6000012fbca0 'TB_Leading_Leading' H:|-(8)-[_UIModernBarButton:0x7f861ff7ed50'Done']   (active, names: '|':_UIButtonBarButton:0x7f861ff7eb70 )>",
    "<NSLayoutConstraint:0x6000012fbcf0 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x7f861ff7ed50'Done']-(0)-|   (active, names: '|':_UIButtonBarButton:0x7f861ff7eb70 )>",
    "<NSLayoutConstraint:0x6000012f0000 'UISV-canvas-connection' UILayoutGuide:0x6000008a3020'UIViewLayoutMarginsGuide'.leading == _UIButtonBarButton:0x7f861ff7be90.leading   (active)>",
    "<NSLayoutConstraint:0x6000012f0230 'UISV-canvas-connection' UILayoutGuide:0x6000008a3020'UIViewLayoutMarginsGuide'.trailing == _UIButtonBarButton:0x7f861ff7eb70.trailing   (active)>",
    "<NSLayoutConstraint:0x6000012f0280 'UISV-spacing' H:[_UIButtonBarButton:0x7f861ff7be90]-(0)-[UIView:0x7f861ff7ea00]   (active)>",
    "<NSLayoutConstraint:0x6000012f02d0 'UISV-spacing' H:[UIView:0x7f861ff7ea00]-(0)-[_UIButtonBarButton:0x7f861ff7eb70]   (active)>",
    "<NSLayoutConstraint:0x6000012e0550 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x6000008a3020'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIButtonBarStackView:0x7f8620b43290 )>",
    "<NSLayoutConstraint:0x6000012e05f0 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x6000008a3020'UIViewLayoutMarginsGuide']-(0)-|(LTR)   (active, names: '|':_UIButtonBarStackView:0x7f8620b43290 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000012fbcf0 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x7f861ff7ed50'Done']-(0)-|   (active, names: '|':_UIButtonBarButton:0x7f861ff7eb70 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.```




【问题讨论】:

  • 您的视图约束不正确,检查每个视图约束并修复它。限制情节提要中发生的重叠。

标签: ios swift uipickerview nslayoutconstraint


【解决方案1】:

自动布局警告与“内部”UIKit 元素有关。

您可以通过替换此行来避免这些警告:

let toolBar = UIToolbar()

用这个:

let toolBar: UIToolbar = {
    let v = UIToolbar(frame: CGRect(origin: .zero, size: CGSize(width: 200.0, height: 44.0)))
    return v
}()

这将使用200 的宽度初始化工具栏......它不需要特别是200,它只需要足够宽以适合您的UIBarButtonItem 按钮的宽度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多