0

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.

Sarah Guo
  • 301
  • 1
  • 3
  • 15

1 Answers1

1

It seems like I need to replace

self.navigationItem.leftBarButtonItem = backButton

with

navBar.topItem?.leftBarButtonItem = backButton
Sarah Guo
  • 301
  • 1
  • 3
  • 15