【问题标题】:Unable to simultaneously satisfy constraints with keyboard and UIToolBar无法同时满足键盘和 UIToolBar 的约束
【发布时间】:2020-06-18 16:40:45
【问题描述】:

我有表格视图,下面有文本视图。我在键盘上方添加了一个工具栏来显示完成按钮。当我点击一行中的按钮以删除该行时,它会显示 LayoutConstraints 问题,如下所示。以下日志还显示了事件的流程。

我可以确认此问题与工具栏有关,如果我删除工具栏则不会出现此问题。

https://github.com/hackiftekhar/IQKeyboardManager/issues/1616 讨论了类似的问题 我从那里尝试了一些建议。

  • 禁用文本视图的自动更正 -> 对我不起作用

  • 使用此代码创建对我不起作用的工具栏

let toolBar = UIToolbar(frame: CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.size.width, height: 44.0)))

有什么办法吗?

textViewShouldBeginEditing
textViewDidBeginEditing
deleteButtonTapped
textViewDidEndEditing
textViewShouldBeginEditing
[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.
(
    "<NSLayoutConstraint:0x280c45f90 'accessoryView.bottom' _UIRemoteKeyboardPlaceholderView:0x10df221b0.bottom == _UIKBCompatInputView:0x10f819ff0.top   (active)>",
    "<NSLayoutConstraint:0x280c66cb0 'assistantHeight' TUISystemInputAssistantView:0x10aa64390.height == 45   (active)>",
    "<NSLayoutConstraint:0x280c44500 'assistantView.bottom' TUISystemInputAssistantView:0x10aa64390.bottom == _UIKBCompatInputView:0x10f819ff0.top   (active)>",
    "<NSLayoutConstraint:0x280c444b0 'assistantView.top' V:[_UIRemoteKeyboardPlaceholderView:0x10df221b0]-(0)-[TUISystemInputAssistantView:0x10aa64390]   (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x280c444b0 'assistantView.top' V:[_UIRemoteKeyboardPlaceholderView:0x10df221b0]-(0)-[TUISystemInputAssistantView:0x10aa64390]   (active)>

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.
textViewDidBeginEditing
textViewDidEndEditing

添加完成按钮的代码。

override func viewDidLoad() {
    super.viewDidLoad()
    //...
    
    self.textView.addDoneButton(title: "Done", target: self, selector: #selector(tapDone(sender:)))
}

extension UITextView {
    
    // Add done button above keyboard
    func addDoneButton(title: String, target: Any, selector: Selector) {
        
        let toolBar = UIToolbar(frame: CGRect(x: 0.0,
                                              y: 0.0,
                                              width: UIScreen.main.bounds.size.width,
                                              height: 44.0))
        toolBar.backgroundColor = .toolBarBackground
        let flexible = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        let barButton = UIBarButtonItem(title: title, style: .plain, target: target, action: selector)
        barButton.setTitleTextAttributes([NSAttributedString.Key.font : UIFont.bodyBold, NSAttributedString.Key.foregroundColor : UIColor.purpleColour], for: [])
        toolBar.setItems([flexible, barButton], animated: false)
        self.inputAccessoryView = toolBar
    }
}

【问题讨论】:

    标签: ios swift uitextview uitoolbar


    【解决方案1】:

    这是 Apple 的错误,不是你的。忽略它。这是一个普遍的“问题”,但对此无能为力;没有记录到视觉伤害。这只是一个烦人的控制台转储。

    【讨论】:

    • 我明白了。 Tx 回复。我确实有一些视觉失真,因为当此错误显示时,我看到我的顶视图和标签栏之间存在空间。不确定它是来自我的代码还是由于错误,我必须对其进行调查。
    • 我不知道“视觉失真”。我只能告诉你,你正在制作一个工具栏并以完全正常的方式填充它。并且每个键盘附件视图工具栏都有这个问题。避免它的唯一可靠方法是不要以这种方式使用工具栏。
    • Tx 进行澄清。工具栏是我最后的选择,因为我一直在努力寻找以交互方式关闭键盘的解决方案。
    • 好吧,你不需要工具栏,只需一个带有按钮的视图(或 按钮的视图)。
    • 这里是一些示例代码:github.com/mattneub/Programming-iOS-Book-Examples/blob/… 你会注意到我有一个完成按钮但没有工具栏。
    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2014-10-27
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多