Reputation: 311
I am trying to add a navigation bar and back button to the top of a MessageViewController in MessageKit. However, the UIBarButtonItem for back button fails to show up. Below is my code
func setUpNavBar() {
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 45, width: UIScreen.main.bounds.width, height: 44))
self.view.addSubview(navBar)
navBar.items?.append(UINavigationItem(title: "XXXX"))
let backButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(onCancel))
self.navigationItem.leftBarButtonItem = backButton
}
The above function is called in viewDidLoad()
. I think my code is correct I just can't seem to find a reason why the button won't show up. Thanks.
Upvotes: 0
Views: 463
Reputation: 311
It seems like I need to replace
self.navigationItem.leftBarButtonItem = backButton
with
navBar.topItem?.leftBarButtonItem = backButton
Upvotes: 1