简体   繁体   English

如何设置导航栏的后项

[英]How to set navigation bar's back item

I have a navigation bar, which is presented modally from another view controller so the back item is not shown, 我有一个导航栏,该导航栏是从另一个视图控制器以模态显示的,因此不会显示后面的项目,

I need to cross dissolve to the second view controller, and put a navigation bar on it without putting one on the first view controller, on the navigation bar of the second view controller the back button should look exactly like system back button. 我需要交叉溶解到第二个视图控制器,并在其上放置一个导航栏,而不要在第一个视图控制器上放置导航栏,在第二个视图控制器的导航栏上,后退按钮应该看起来与系统后退按钮完全一样。

The back item would appear automatically when you push a ViewController. 当您按下ViewController时,后一项将自动出现。 When you present it modally you would do it outside the initial navigation controller, therefore it's navigation would get hidden by the new one. 模态显示时,您可以在初始导航控制器之外进行操作,因此新的导航器将隐藏它的导航。

Edition with the proposed solution: Embed the first UIViewController in a UINavigationViewController and push the second instead of presenting it modally. 带有建议解决方案的版本:将第一个UIViewController嵌入UINavigationViewController中,然后推送第二个UIView而不是模态呈现。

If you have navigation bar and you need custom back button, you can add it like this: 如果您有导航栏,并且需要自定义后退按钮,则可以这样添加它:

let button = UIBarButtonItem(title: "Back Title", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(someMethod))
self.navigationItem.leftBarButtonItem = button

And in your selector you can dismiss your current viewController or call some other back method 在选择器中,您可以关闭当前的viewController或调用其他back方法

EDIT 编辑

If you need some custom animation with standart push transition style you can do next: 如果您需要一些具有标准推送过渡样式的自定义动画,则可以执行以下操作:

For example create viewController in storyboard with SecondViewController id 例如在带有SecondViewController id的情节提要中创建viewController

At your transition action method make some like this: 在您的过渡动作方法中进行如下操作:

func showSecondViewController() {
    guard navigationController != nil else {
        return
    }
    guard let secondViewController = storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") else {
        return
    }
    navigationController!.pushViewController(secondViewController, animated: false)
    UIView.transitionWithView(navigationController!.view, duration: 1, options: .TransitionCrossDissolve, animations: nil, completion: nil)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM