I'm trying to create custom titleView and change the position to be next to the back button.
The titleView should be look like :
So what i did was :
private func setupNavigationItems() {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Info"
label.textColor = UIColor.black
label.textAlignment = .left
navigationItem.titleView = label
if let navigationBar = navigationController?.navigationBar {
label.widthAnchor.constraint(equalTo: navigationBar.widthAnchor, constant: -40).isActive = true
}
self.navigationController?.navigationBar.tintColor = UIColor.black
}
override func updateViewConstraints() {
super.updateViewConstraints()
setupNavigationItems()
}
It works, but in some situation i get crash :
*** 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.'
How can i fix this crash?, there is a better way to change the position of the navigation title?
Thanks