隐藏后退按钮嵌入式导航控制器 swift 3

hide backbutton embedded UINavigationController swift3

我正在使用带 Xcode8Swift3 的嵌入式导航控制器,我本可以做一些更改,例如透明背景等,但无法隐藏 backbutton 或更改其标题

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

如何自定义导航栏中的后退按钮?

谢谢

设计自定义 UIButton 并用它替换默认 NavigationBar BackBarButtonItem

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)
    }
}

您可以在 viewWillAppear 下编写此代码:

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

如果您想添加图片,您可以这样做:

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