Layout Constraint Errors with simple UIToolar for keyboard inputAccessoryView

Change

let aKeyboardToolBar = UIToolbar()

to

let aKeyboardToolBar = UIToolbar(frame:CGRect(x:0, y:0, width:100, height:100))

That way it has some size to work with as you are giving it content.


Since I don't need the autocorrection my solution was simply to set

textField.autocorrectionType = .no

In the app I'm writing right now the user can go back and forth between filling various textFields which can break the UIToolbar constraints.

I hope it can help someone!


I was getting the

Unable to simultaneously satisfy constraints

error with the UIToolbar in my keyboard inputAccessoryView, but my UIToolbar's dimensions were already defined at instantiation:

@objc var accessoryDoneButton: UIBarButtonItem!
@objc var emptyButton: UIBarButtonItem!
@objc let accessoryToolBar = UIToolbar(frame: CGRect(x:0, y: 0,width:UIScreen.main.bounds.width, height:44))

I fixed my problem by altering the items in the UIToolBar:

    // Position dismiss button in the 3rd. space from the left side of toolbar.
//        self.accessoryToolBar.items = [self.emptyButton, self.emptyButton, self.emptyButton, self.accessoryDoneButton]
        self.accessoryToolBar.items = [self.accessoryDoneButton]
        self.symptomDescription.inputAccessoryView = self.accessoryToolBar