xcode 您如何确定自定义 UINavigationItem.titleView 的大小/框架?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8287894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:34:02  来源:igfitidea点击:

How do you determine the size/frame of a custom UINavigationItem.titleView?

objective-cxcodeuinavigationbaruinavigationitem

提问by ChrisP

After creating a custom view and assigning it to the navigationItem.titleView property it is displayed like this

创建自定义视图并将其分配给 navigationItem.titleView 属性后,它显示如下

enter image description here

在此处输入图片说明

with the custom view filling the space between the two buttons. Therefore, the custom view is not centered on the navigation bar. How do I determine the frame of the view in the .titleView property? I want to center some text in the navigation bar, say under the time stamp.

自定义视图填充了两个按钮之间的空间。因此,自定义视图不会以导航栏为中心。如何在 .titleView 属性中确定视图的框架?我想在导航栏中居中一些文本,比如在时间戳下。

回答by rob mayoff

If you really want to get titleView's frame (in your top-level view's coordinate space), you can do this:

如果你真的想得到titleView的框架(在你的顶级视图的坐标空间中),你可以这样做:

[self.navBar layoutIfNeeded];
CGRect titleViewFrameInTopLevelViewSpace = [self.navigationItem.titleView
    convertRect:self.navigationItem.titleView.bounds
    toView:self.view];

You need to do layoutIfNeededif you have just assigned titleView, because by default the navigation bar won't lay out its subviews until the next pass through the run loop.

layoutIfNeeded如果您刚刚分配了titleView,则需要这样做,因为默认情况下导航栏不会在下一次通过运行循环之前布置其子视图。

That said,the titleViewwill be centered automatically, if it fits. I think you are setting the frame (or bounds) of your custom view too large. I tested this two ways:

这就是说,titleView将自动居中,如果它符合。我认为您将自定义视图的框架(或边界)设置得过大。我测试了这两种方式:

  • I set up the titleViewdirectly in the XIB. I simply dragged a View from the Object library onto the center of the navigation bar:
    XIB screen shot
    It sized the view to 128x33 automatically. The resize handles let me adjust the size. It stays centered until it overlaps the Categorize button. Then it shifts left.

  • I set the titleViewproperty in viewDidLoad:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 33)];
        customView.backgroundColor = [UIColor greenColor];
        self.navItem.titleView = customView;
     }
    

    The result looks like this:
    Simulator screen shot

  • titleView直接在XIB中设置了。我只是将一个视图从对象库拖到导航栏的中心:
    XIB screen shot
    它自动将视图大小调整为 128x33。调整大小手柄让我可以调整大小。它保持居中,直到与“分类”按钮重叠。然后它向左移动。

  • 我将titleView属性设置为viewDidLoad

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 33)];
        customView.backgroundColor = [UIColor greenColor];
        self.navItem.titleView = customView;
     }
    

    结果如下所示:
    Simulator screen shot

回答by mattjgalloway

You could get the width of the leftBarButtonItemand the rightBarButtonItemafter you've set them, and then use that to determine how to centre within the view you supply to titleView. That might do what you want?

你可以得到的宽度leftBarButtonItemrightBarButtonItem你设置后它们,然后用它来确定如何向你提供给视图内中心titleView。这可能会做你想要的?