我似乎无法以编程方式正确设置navigationController的后退按钮,以显示上一个视图何时使用
self.navigationController?.pushViewController(newView, animated: true)
我使用循环隐藏了前一个视图中的所有视图viewDidDisappear
,并且在viewDidAppear中显示的新视图中,我尝试以各种方式设置后退按钮的操作;然而,虽然我可以成功操作自动显示的后退按钮,例如隐藏它或更改它的图像,但我无法设置它的动作。
任何见解都会受到赞赏,因为我找到的答案似乎都没有正常工作。这也是在不使用故事板的情况下完成的
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(_:)))
答案 0 :(得分:1)
在您的情况下,在导航中添加自定义按钮。
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,
}
}