gpt4 book ai didi

ios - 在导航 Controller 中设置后退按钮

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

我似乎无法以编程方式正确设置 navigationController 的后退按钮,该按钮在上一个 View 使用时显示

self.navigationController?.pushViewController(newView, animated: true)

我使用循环在 viewDidDisappear 中隐藏了之前 View 中的所有 View ,在 viewDidAppear 中呈现的新 View 中,我尝试以各种方式设置后退按钮的操作;但是,虽然我可以成功地操纵自动显示的后退按钮(例如隐藏它或更改它的图像),但我无法设置它的操作。

任何见解都将不胜感激,因为我发现的答案似乎都无法正常工作。这也是在不使用 Storyboard的情况下完成的

if let img = UIImage(named: "backButton") {
self.navigationController?.navigationBar.backIndicatorImage = img
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = img
print("IMAGE")
}
topItem.backBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Rewind, target: self,
action:#selector(self.backButtonAction(_:)))

最佳答案

在您的情况下,在导航上添加一个自定义按钮。

class YourViewController: UIViewController {
//Navigation Items.

//left bar button item.
private var leftBarButtonItem : UIBarButtonItem!

//left button.
private var navigationLeftButton : UIButton!

//Your other variable/object declaration.

func viewDidLoad() {
super.viewDidLoad()
self.leftBarButtonItem = UIBarButtonItem()
}

func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

self.setNavigationBackButton()
}



private func setNavigationBackButton() {

if(self.navigationLeftButton == nil) {
self.navigationLeftButton = UIButton(type: UIButtonType.System)
}


//Styling your navigationLeftButton goes here...

self.navigationLeftButton.addTarget(self, action: Selector("backButtonTapped"), forControlEvents: UIControlEvents.TouchUpInside)
self.leftBarButtonItem.customView = self.navigationLeftButton
self.navigationItem.leftBarButtonItem = self.leftBarButtonItem
}

func backButtonTapped(AnyObject:sender) {
// Here add your custom functionalities.
// Note, this will not pop to previous viewcontroller,

}
}

关于ios - 在导航 Controller 中设置后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38442348/

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