简体   繁体   English

改善需要UINavigationBar的UIViewControllers的可移植性

[英]Improving the portability of UIViewControllers that require a UINavigationBar

We often try to reuse our view controllers, whether they get pushed to a navigation controller or are presented. 我们经常尝试重用我们的视图控制器,无论它们被推送到导航控制器还是被呈现。 However, things can get a little hairy when an explicit UINavigationBar is part of the view controller's function design. 但是,当显式UINavigationBar是视图控制器功能设计的一部分时,事情可能会变得有些繁琐。

If we just set the view controller's navigation item, pushing to a navigation controller works as expected, but presenting results in no navigation bar at all. 如果我们只是设置视图控制器的导航项,则按预期的方式将其推入导航控制器,但根本不会在导航栏中显示结果。 On the other hand, if we explicitly add and configure a UINavigationBar to the view, presenting works fine, but pushing results in double navigation bars. 另一方面,如果我们向视图中明确添加并配置UINavigationBar,则呈现效果很好,但是将结果推送到双导航栏中。 While we could specify the parent navigation controller's navigation bar to be hidden for that view, it creates a clumsy animated transition when pushing or popping that view controller. 虽然我们可以指定要为该视图隐藏父级导航控制器的导航栏,但是当按下或弹出该视图控制器时,它会创建笨拙的动画过渡。

(In a perfect world, I would imagine that the navigationBar property would be managed by UIViewController instead of UINavigationController. Alas, that's not the case, so here I am.) (在一个完美的世界中,我可以想象的是,navigationBar属性将由UIViewController而不是UINavigationController来管理。可惜,事实并非如此,所以在这里。)

What are some of the best practices people here have found to maintain the portability of view controllers requiring a UINavigationBar? 人们在这里发现了哪些最佳实践来维护需要UINavigationBar的视图控制器的可移植性?

I've handled this in one of two ways: 我用以下两种方法之一来处理:

  1. I fall back to asserting viewDidAppear: that self.navigationController is non-nil. 我回过头来断言viewDidAppear: self.navigationController不为nil。 Push the responsibility of providing a UINavigationController to wrap your view controller instance to clients of the class. 承担提供UINavigationController的责任,以将视图控制器实例包装到该类的客户端。

  2. Alternatively, you can embed a UINavigationBar instance and manage it yourself, just remember to hide the possible navigation bar from your containing navigation controller in viewWillAppear: . 另外,您可以嵌入UINavigationBar实例并自己管理它,只需记住在viewWillAppear:从包含的导航控制器中隐藏可能的导航栏。

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

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