backBarButtonItem显示图像不正确

时间:2018-07-16 16:33:09

标签: ios swift uinavigationbar back-button uinavigationitem

我正在将自定义UIBarButtonItembackBarButtonItem的图片一起使用。我发现了奇怪的行为,因为除了我的图像之外,还显示了默认的“箭头”图标。

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        navigationItem.backBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "BackButtonIcon"), style: .plain, target: nil, action: nil)
    }

    @IBAction private func push(_ button: UIButton) {
        let secondViewController = SecondViewController()
        navigationController?.pushViewController(secondViewController, animated: true)
    }

}

class SecondViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
    }

}

结果是 enter image description here UI debugger

2 个答案:

答案 0 :(得分:1)

尝试使用:

navigationBar.backIndicatorImage = #imageLiteral(resourceName: "BackButtonIcon")

navigationController?.navigationBar.backIndicatorTransitionMaskImage = #imageLiteral(resourceName: "BackButtonIcon")

navigationItem.backBarButtonItem = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.plain, target: nil, action: nil)

导航栏具有用于背面图像的属性以及作为UINavigationItem的backItem属性。您可以阅读更多here

编辑:

在源视图控制器而不是目标控制器中使用此代码。

答案 1 :(得分:0)

self.navigationItem.setHidesBackButton(true, animated: true)viewDidLoad中尝试viewWillAppear。 编辑:这将隐藏本机的后退按钮。之后您应该可以设置自己的了。