Override back button in navigation stack while keeping appearance of default back button?

后端 未结 8 2073
野的像风
野的像风 2020-12-08 22:38

How do I override the back button for just one view (not for all the back buttons present in different views) such that on click of the back button, root view controller is

8条回答
  •  無奈伤痛
    2020-12-08 23:07

    Following Sarasranglt's method of having a transparent button, in objective C, and Pavle Mijatovic's earlier swift version, here is a swift 4 version:

    let transparentButton = UIButton()
    transparentButton.frame = CGRect(x:0, y:0, width:50, height: 40)
    transparentButton.backgroundColor = UIColor.clear
    transparentButton.addTarget(self, action:#selector(backAction(sender:)), for:.touchUpInside)
    self.navigationController?.navigationBar.addSubview(transparentButton)
    

    and

    @objc func backAction(sender:UIButton) {
    // Some action
    }
    

提交回复
热议问题