简体   繁体   中英

Add a Custom Back Button to a Custom Navigation Bar

I have been trying to add a custom back button to a Nav Bar that is being created on Load as I am using a Multi-Storyboard set-up, so the back button will not appear automatically, plus I am trying to use a custom image for it.

This is what I have so far:

class AddFundsMethodTable : UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func viewWillAppear(animated: Bool) {
    self.styleNavBar()
}

func styleNavBar() {
    var newNavBar = UINavigationBar(frame: CGRectMake(0,0, CGRectGetWidth(self.view.frame), 100.0))
    newNavBar.tintColor = UIColor.whiteColor()

    var navItem = UINavigationItem(title: "Choose a Method")
    var backImage = UIImage(named: "backBtn")
    var backItem = UIBarButtonItem(image: backImage, style: .Plain, target: self, action: "backButton:")
    navItem.leftBarButtonItem = backItem
    newNavBar.setItems([navItem], animated: false)

    self.view.addSubview(newNavBar)
}

func backButton(sender: UIBarButtonItem) {
    self.navigationController?.popToRootViewControllerAnimated(true)
}

}

In ViewDidLoad write this:

override func viewDidLoad() {
  super.viewDidLoad()
  self.styleNavBar()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM