Create a custom left back button on UINavigationBar WITH the standard arrow on the left

后端 未结 6 1594
情书的邮戳
情书的邮戳 2021-02-05 03:27

When I create a custom back button, I use the following code:

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@\"Yeah\" style:UIBarButton         


        
6条回答
  •  北恋
    北恋 (楼主)
    2021-02-05 04:22

    For Swift, using Erzekiel's answer as the basis for this, you can simplify it to -

    extension UIViewController {
    
        func setBackButtonTitle(to title: String) {
            let barButtonItem = UIBarButtonItem(title: title, style: .plain, target: self, action: nil)
            self.navigationItem.backBarButtonItem = barButtonItem
        }
    }
    

    This should be called from the parent viewController, eg in viewWillDisappear -

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.setBackButtonTitle(to: "Back"))
    }
    

提交回复
热议问题