如何获取 UINavigationBar 中后退按钮的宽度? [英] How do I get the width of the back button in a UINavigationBar?

查看:19
本文介绍了如何获取 UINavigationBar 中后退按钮的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个控制器,它可以从多个位置推动,因此它的后退按钮可能具有多个标题之一.它还有一个按钮,可以弹出另一个控制器.它们都有相同的自定义标题元素,但它们的居中不同,因为第二个控制器没有后退按钮.我想添加一个垫片来解决这个问题,但我不知道要制作什么宽度.如何获取 UINavigationBar 中后退按钮的宽度?

We have a controller that might be pushed from several places, so its back button might have one of several titles. It also has a button that pops up another controller. They both have the same custom title element, but they're centred differently because the second controller has no back button. I'd like to add a spacer to fix this, but I don't know what width to make it. How do I get the width of the back button in a UINavigationBar?

推荐答案

你总是可以探索导航栏的子视图,寻找后退按钮视图并在找到时选择大小.不久前我写了一篇关于迭代 UIView 子视图的博文,这里是 URL:

You could always spelunk through the navigation bar's subviews, looking for the back button view and picking off the size when you find it. I did a blog post a while back on iterating through subviews of a UIView, here is the URL:

http://www.dosomethinghere.com/2011/07/09/logging-the-view-hierarchy/

我在当前应用程序中使用上述 URL 中描述的 InspectView 类,当我将此代码插入到导航栏中有后退按钮的视图控制器中时:

I am using the InspectView class described in the above URL in my current app, and when I inserted this code into a view controller that has a back button in the navigation bar:

- (void)viewDidAppear:(BOOL)animated
{
    [InspectView dumpViewToLog:self.navigationController.navigationBar findParent:NO];
}

这是控制台中的结果输出:

This is the resulting output in the console:

2011-09-20 09:11:19.599 MyApp[2070:207] 
Inspect view hierarchy -----------------------------------
UINavigationBar (0x6626460): frame origin: (0, 20) size: (320, 44) [tag=0] UIView : UIResponder : NSObject : 
.   UILabel (0x6613f00): frame origin: (62, 0) size: (200, 44) [tag=0] UIView : UIResponder : NSObject : 
.   .   UILabel (0x6628520): frame origin: (0, 19) size: (200, 20) [tag=0] UIView : UIResponder : NSObject : 
.   .   UILabel (0x665eda0): frame origin: (0, -1) size: (200, 24) [tag=0] UIView : UIResponder : NSObject : 
.   UINavigationItemButtonView (0x662a5b0): frame origin: (5, 7) size: (49, 30) [tag=0] UINavigationItemView : UIView : UIResponder : NSObject : 
End of view hierarchy -----------------------------------

UINavigationItemButtonView 是返回按钮,你可以看到它的大小是 49 宽 x 30 高.你会在你的应用中寻找这个视图类并记住它的大小.

The UINavigationItemButtonView is the back button, and you can see that the size if 49 wide by 30 tall. You would look for this view class in your app and remember the size.

请记住,将此代码放入 viewDidLoad 或 viewWillAppear 会显示不同的结果.

Please keep in mind that putting this code into viewDidLoad or viewWillAppear will show different results.

这篇关于如何获取 UINavigationBar 中后退按钮的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆