简体   繁体   English

iOS - 仅为部分自定义导航栏 UIViewController

[英]iOS - Customize Navigation Bar for only some UIViewController

I have to add new screens to an existing app, that will use a different navigation bar style than the one that already exists and will be accessed from different screens.我必须向现有应用程序添加新屏幕,该应用程序将使用与已经存在的导航栏样式不同的导航栏样式,并且可以从不同的屏幕访问。 The idea would be to show the new Navigation Bar style only for those screens, so when the user finishes that flow or goes back to the screen that started the navigation, it should show again the navigation bar style it was previously using.想法是只为这些屏幕显示新的导航栏样式,因此当用户完成该流程或返回开始导航的屏幕时,它应该再次显示它之前使用的导航栏样式。

I have tried 2 things but didn't work as expected:我尝试了两件事,但没有按预期工作:

  1. I thought of wrapping the new screens in a new UINavigationController , so I could change its navigation bar style and it would be consistent for the new screens.我想把新屏幕包装在一个新的UINavigationController中,这样我就可以改变它的导航栏样式,它对于新屏幕来说是一致的。 It works but the problem is that I am not able to customize the initial UINavigationController transition to make it not look like a modal (ie I want to show that UINavigationController with the same animation as if I were pushing a UIViewController ). It works but the problem is that I am not able to customize the initial UINavigationController transition to make it not look like a modal (ie I want to show that UINavigationController with the same animation as if I were pushing a UIViewController ). Is there a way to do that?有没有办法做到这一点? By the way, I am managing the navigations with storyboards segues.顺便说一句,我正在使用故事板 segues 管理导航。

  2. I also thought of using我也想过用

self.navigationController?.navigationBar.isHidden = true

But doesn't seem clean because I would have to show it again when the flow is finished (it is a bit long) or cancelled.但看起来并不干净,因为当流程完成(有点长)或取消时,我必须再次显示它。 This makes a lot of combinations and it would be easy to miss one of them, so this doesn't seem a practical solution.这产生了很多组合,很容易错过其中一个,所以这似乎不是一个实际的解决方案。 Is there a better way to do this?有一个更好的方法吗?

The presenting view controller is where you want to change the nav bar for the controller to be presented. presenting view controller是您要更改要呈现的 controller 的导航栏的位置。 So you'd make any changes to the navbar just before you call push(viewController:animated:) .因此,您可以在调用push(viewController:animated:)之前对导航栏进行任何更改。

And then in viewWillAppear of still the presenting view controller you would reset the navbar to what it was initially.然后在仍然presenting view controllerviewWillAppear中,您将导航栏重置为最初的样子。

NOTE: Keep in mind that depending on the kind of changes you're doing to the navbar, the transition might no longer be smooth.注意:请记住,根据您对导航栏所做的更改类型,转换可能不再平滑。 The user might see some flickering of sorts on the navbar, which would be poor UI/UX.用户可能会在导航栏上看到一些闪烁,这将是糟糕的 UI/UX。

Edit: Another alternative编辑:另一种选择

Alternatively, you could get rid of the navbar and instead implement your own header view that you can style however you want, to represent the navbar.或者,您可以摆脱导航栏,而是实现自己的header view ,您可以根据需要设置样式来表示导航栏。

  • You can style it as a simple navbar with a title + back button.您可以将其设置为带有标题 + 后退按钮的简单导航栏。
  • You can style it to look like a navbar with large title您可以将其设置为看起来像带有大标题的导航栏
  • You can style it as a navbar with back button + background image + title + right bar buttons您可以将其设置为带有后退按钮 + 背景图像 + 标题 + 右栏按钮的导航栏

As you can see, this second alternative offers you more freedom in what you can do.如您所见,第二种选择为您提供了更多的自由。

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

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