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

查看:102
本文介绍了如何获取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宽,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天全站免登陆