gpt4 book ai didi

ios - 添加自定义后退按钮对 UINavigationBar 不起作用

转载 作者:行者123 更新时间:2023-11-28 06:03:43 25 4
gpt4 key购买 nike

我正在尝试向我的导航 Controller 添加自定义后退按钮,但它无法正常工作。

func setupNavBarItems() {
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = UIColor.clear

let backButton = UIButton(type: .system)
backButton.setImage(#imageLiteral(resourceName: "icon_back"), for: .normal)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}

而且我也尝试过这个但没有任何运气(我用以下代码替换了底部的两行)

    let backButton = UIButton(type: .system)
backButton.setImage(#imageLiteral(resourceName: "icon_back"), for: .normal)
let backBarButton = UIBarButtonItem.init(customView: backButton)
navigationItem.setLeftBarButton(backBarButton, animated: true)

我看到了自定义按钮图像,但它不会触发后退按钮操作。

最佳答案

在这种情况下,最好将其声明为实例变量,因为除了目标 popViewController 之外,目标将被局部变量/lazy 丢失

let addButton = UIBarButtonItem(image:UIImage(named:"your_icon_name"), style:.plain, target:self, action:#selector(YourControllerName.buttonAction(_:)))
addButton.tintColor = UIColor.white
self.navigationItem.leftBarButtonItem = addButton

.....

@objc func buttonAction(_ sender: UIBarButtonItem) {
self.navigationController?.popViewController(animated: true)
}

关于ios - 添加自定义后退按钮对 UINavigationBar 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48836379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com