Sarah Guo
Sarah Guo

Reputation: 311

UIBarButtonItem Doesn't Show Up When Using MessageKit

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

Answers (1)

Sarah Guo
Sarah Guo

Reputation: 311

It seems like I need to replace

self.navigationItem.leftBarButtonItem = backButton

with

navBar.topItem?.leftBarButtonItem = backButton

Upvotes: 1

Related Questions