UINavigationController “back button” custom text?

后端 未结 16 1458
Happy的楠姐
Happy的楠姐 2020-11-28 01:41

The \"back button\" of a UINavigationController by default shows the title of the last view in the stack. Is there a way to have custom text in the back button

16条回答
  •  春和景丽
    2020-11-28 02:24

    if You want to set title in ARRIVING controller (sometimes more logic..) in swift 3 do:

    func setBackButtonNavBar(title: String, delay: Double){
    
        let when = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: when, execute: { () -> Void in
    
            if let navBar = self.navigationController?.navigationBar{
                navBar.backItem?.title = title
            }
        })
    
    }
    

    in upcoming controller:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setBackButtonNavBar(title: "back", delay: 0.3)
    }
    

    usually I put self.setBackButtonNavBar in a controller extension.

提交回复
热议问题