Unable to simultaneously satisfy constraints with keyboard and UIToolBar
Asked Answered
L

1

7

I've table view below which I've text view. I'm adding a tool bar above keyboard to show Done button. When I tap on a button in a row to delete the row it shows LayoutConstraints issue as shown below. Following log also shows the flow of event.

I can confirm that this issue is related with tool bar, if I remove tool bar then this problem doesn't appear.

Similar issue is discussed on https://github.com/hackiftekhar/IQKeyboardManager/issues/1616 I've tried few suggestions from there viz.

  • Disable auto correct for text view -> Didn't work for me

  • Use this code for creating ToolBar which didn't work for me

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

Any fix?

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

Code for add done button.

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
    }
}
Leavy answered 18/6, 2020 at 16:40 Comment(0)
R
25

This is Apple's bug, not yours. Ignore it. It's a widespread "issue" but there's nothing to be done about it; no visual harm is registered. It's just an annoying console dump.

Roustabout answered 18/6, 2020 at 16:42 Comment(7)
I see. Tx for reply. I do have some visual distortion as I see space between my top view and Tab bar when this error shows. Not sure if it is coming from my code or it is due to bug, I've to investigate it.Leavy
I don't know about the "visual distortion". All I can tell you is that you are making a toolbar and populating it in a completely normal way. And that every keyboard accessory view toolbar is having this issue. The only reliable way to avoid it is not to use a toolbar in this way.Roustabout
Tx for clarification. Tool bar was my last option as I've been struggling to find a solution to dismiss keyboard interactively.Leavy
Well you don't need a toolbar for that, just a view with a button (or a view that is a button).Roustabout
Here's some sample code: github.com/mattneub/Programming-iOS-Book-Examples/blob/… You'll notice that I've got a Done button but no toolbar.Roustabout
Awesome, will check it out.Leavy
Just to confirm that the space issue has nothing to do with tool bar as I tried using UIView and I see same space issue.Leavy

© 2022 - 2024 — McMap. All rights reserved.