如何可靠地将内置 UINavigationController UIToolbar 放置在 iPad 视图之上
我试图在 iPad 应用程序中使用 UINavigationController 对象的内置 UIToolbar 对象,但我希望它显示在视图的顶部而不是底部(这是它的默认位置)。
我还隐藏了 UINavigationController 对象的导航栏。
为了使这项工作正常进行,我必须编写以下代码:
navigationController.navigationBarHidden = YES;
navigationController.toolbarHidden = NO;
navigationController.toolbar.frame = CGRectMake(0, 0, 768, 44);
该解决方案有一个例外:当应用程序进入后台并再次变为活动状态时,工具栏始终重新定位在视图的底部。
我尝试将代码从 viewDidLoad 移动到 viewDidAppear:animated,但它的行为仍然如此。
首先,是否有更好的方法来解决这个问题,如果没有,如何阻止工具栏被重新定位?
我还决定使用自己的 UIToolbar 对象,并通过自定义 Base UIViewController 类的 viewDidLoad 将其添加到每个视图。然而,这会导致工具栏在每个视图被推送或弹出时产生动画,因为它实际上是视图的一部分,这看起来“做作”。
关于可能的解决方案有什么想法吗?
谢谢大家!
I am trying to use the UINavigationController object's built-in UIToolbar object in my iPad application, but I want it to be displayed on top of the view instead of the bottom, which is where it defaults.
I am also hiding the UINavigationController object's Navigation Bar.
In order to make this work, I had to write the following code:
navigationController.navigationBarHidden = YES;
navigationController.toolbarHidden = NO;
navigationController.toolbar.frame = CGRectMake(0, 0, 768, 44);
This solution works with one exception: when the application Enters Background and Becomes Active again, the Toolbar is always repositioned on the bottom of the view.
I've tried moving the code from viewDidLoad to viewDidAppear:animated, and it still behaves this way.
First, is there any better way to approach this, and if not, how can I stop the Toolbar from being repositioned?
I've also instead decided to use my own UIToolbar object and add it to each view via a custom Base UIViewController class' viewDidLoad. However, this causes the Toolbar to animate when each view is pushed or popped because it is actually part of the view, which just seems "hokey".
Any ideas on possible solutions?
Thanks everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它在 UINavigationController 的工具栏属性下的文档中说:
这有点黑客,但您可以自己制作一个 UIToolbar 并将其直接添加到窗口(即通过导航控制器)。
It says in the documentation under UINavigationController's toolbar property that:
This is sort of hackish, but you could make a UIToolbar yourself and add it directly to the window (i.e. over the navigation controller.)
您可以使用下面的类别来修改 UIToolbar 类以实现您想要的效果。
You can use the category below to modify the UIToolbar class to achieve what you're after.
与 UINavigationController 一起使用时,工具栏的功能有限。它仅提供了一种管理工具栏中的操作表的便捷方法。
来自文档:“仅向想要从工具栏显示操作表的客户端提供对此工具栏的访问。您不应直接修改 UIToolbar 对象。”
我将使用的解决方案是创建 UIView 的子类,并使用方便的方法来管理您的操作表和您需要的任何其他自定义功能。此自定义视图可以在 UINavigationController 中的所有视图之间共享,并可以放置在父视图中您喜欢的位置。这将使您能够最终控制自定义顶部工具栏。
The toolbar has limited functionality when used with a UINavigationController. It only provides a convenient way to manage the actionsheet in the toolbar.
From the docs: "Access to this toolbar is provided solely for clients that want to present an action sheet from the toolbar. You should not modify the UIToolbar object directly."
The solution I would use is to create a subclass of UIView with convenience methods to manage your actionsheet and any other custom functionality you need. This custom view can be shared across all views in the UINavigationController and placed where ever you like in the parent view. This will give you ultimate control of your custom top placed toolbar.