0
votes

Essentially I am trying to display a UIToolBar above the keyboard once the textfield is selected.

I get the following error when selecting that textfield to edit:

Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'

In viewDidLoad I do the following:

let toolBar = UIToolbar()
var items = [UIBarButtonItem]()

items.append(
    UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
)

items.append(
    UIBarButtonItem(title: "Tool Bar Text", style: .plain, target: self, action: #selector(confirmSignature))
)

items.append(
    UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
)

toolBar.setItems(items, animated: true)
toolBar.tintColor = .white
toolBar.barTintColor = UIColor.red



toolBar.translatesAutoresizingMaskIntoConstraints = false


if #available(iOS 11.0, *) {
    let guide = self.view.safeAreaLayoutGuide
    toolBar.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
    toolBar.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
    toolBar.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
    toolBar.heightAnchor.constraint(equalToConstant: 80).isActive = true

}
else {
    NSLayoutConstraint(item: toolBar, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
    NSLayoutConstraint(item: toolBar, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true
    NSLayoutConstraint(item: toolBar, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true

    toolBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
}

exampleTextField.inputAccessoryView = toolBar

And as an extension I do the following for when the textfield is selected:

extension myTableViewName: UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == exampleTextField {
            let toolBar = UIToolbar()

            view.addSubview(toolBar)
        }
    }
}
2
Check valid constraints is exists.Vineesh TP

2 Answers

0
votes

Comment these lines ( no common view between textfield acessory and view )

let guide = self.view.safeAreaLayoutGuide
toolBar.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
toolBar.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
toolBar.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true

As the toolBar input view will always take the whole width of screen at bottom with the specified height , you can only leave this

toolBar.heightAnchor.constraint(equalToConstant: 80).isActive = true
0
votes

toolBar is not added to the view as the error message states.

self.view.addSubview(toolBar)