Swift Hides back button

677 views Asked by At

I want to hide the back button and set a title.

I'm using the following code:

    override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Einstellungen"
    navigationItem.hidesBackButton = true }

But the title isn't shown and the back button is still there but if I touch it nothing happens. Can anybody help me please?

2

There are 2 answers

0
Philipp Rosengart On BEST ANSWER

I found a solution on my own.

If I'm setting the title and the hidesBackButton from my previous ViewController everything works fine.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let destinationVC = segue.destination as? ViewControllerFirstSettings {

        destinationVC.navigationItem.hidesBackButton = true
        destinationVC.navigationItem.title = "Einstellungen"
    }
}
0
Shobhakar Tiwari On

This code may help :

 // MARK: - CUSTOM METHODS
        func createNavBar() {

            let leftNavigationButton =  UIButton()
            leftNavigationButton.setImage(UIImage(named: "ic_back.png"), forState: .Normal)
            leftNavigationButton.frame = CGRectMake(10, 10, 20, 15)
            leftNavigationButton.addTarget(self, action: "onBackButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
            let customBarItem = UIBarButtonItem(customView: leftNavigationButton)
            self.navigationItem.leftBarButtonItem = customBarItem;

            //set TitleAppIcon
            let GR = UITapGestureRecognizer(target: self, action: Selector("headerLogoTapAction:"))
            let imageView = UIImageView(frame: CGRect(x: 90, y: 0, width: ((UIScreen.mainScreen().bounds.width)/3), height: 40))
            imageView.addGestureRecognizer(GR)
            imageView.userInteractionEnabled = true
            navigationItem.titleView = imageView
        }