Özgür Ersil
Özgür Ersil

Reputation: 7013

hide backbutton embedded UINavigationController swift3

I am using embedded navigationController with Xcode8 and Swift3, i could have done some changes like transparent background etc but can not hide backbutton or change its title

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true

How can i make the backbutton custom in navigation bar?

thanks

Upvotes: 1

Views: 726

Answers (2)

Mathews
Mathews

Reputation: 743

Design a custom UIButton and replace the default NavigationBar BackBarButtonItem with it.

func customBackButton() {
    let leftButton = UIButton(type: UIButtonType.Custom)
    leftButton.frame = CGRectMake(0, 0, 36, 36)
    leftButton.clipsToBounds = true
    leftButton.setTitle("yourTitle", forState: .Normal) //set back button title
    leftButton.setImage(UIImage(named: "yourBackButton.png"), forState: .Normal) // add custom image
    leftButton.addTarget(self, action: #selector(self.onBackButton_Clicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)
    let leftBarButton = UIBarButtonItem()
    leftBarButton.customView = leftButton
    self.navigationItem.leftBarButtonItem = leftBarButton
}

func onBackButton_Clicked(sender: UIButton)
{
    if(webview.canGoBack) {
        webview.goBack()
    }
    else {
        self.navigationController.popViewController(animated: true)
    }
}

Upvotes: 0

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

You could write this code under viewWillAppear:

override func viewWillAppear(_ animated: Bool)
{
    super.viewWillAppear(animated)
    self.navigationItem.hidesBackButton = true
}

If you want to add an image you can do:

let leftButton: UIBarButtonItem = UIBarButtonItem(image: buttonImage, style: UIBarButtonItemStyle.plain, target: self, action:#selector(ViewController.leftButtonPress(sender:)))
navigationItem.leftBarButtonItem = leftButton

Upvotes: 1

Related Questions