【问题标题】:right uiBarButtonItems pushes my titleview to the left右 uiBarButtonItems 将我的 titleview 推到左边
【发布时间】:2014-09-02 02:01:20
【问题描述】:

我在右侧添加了两个 uibarbuttonitem,但它会将 titleview 推到左侧。

如果标题视图太长,我希望它保持居中并被截断

【问题讨论】:

  • 请在这个问题中添加iOS标签

标签: ios ios7 uinavigationbar uibarbuttonitem


【解决方案1】:

下面的方法可以做到这一点:保持居中 + 截断如果太长。

在左侧添加相同数量的固定空格(在您的情况下为 2 个)。它可能需要您:

  1. 替换最左边的UIBarButtonItem
  2. 使用navigationItem.titleView(您已经是):在左侧添加相等数量的UIBarButtonItemUIBarButtonSystemItemFixedSpace

注意:要达到完美居中,严格使用21.5的宽度,保守使用22.0的宽度:

// Adding invisible spaces to the left
UIBarButtonItem * space = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
    target:nil action:nil];
space.width = 21.5; // Exact match
[self.navigationItem setLeftBarButtonItems:@[space,space]];

仅供参考,标题视图应如下所示:

self.navigationItem.titleView = ({
    UILabel * titleView = [[UILabel alloc]
        initWithFrame:CGRectMake(0, 0, 200, 20)];
    [titleView setTextAlignment:NSTextAlignmentCenter];

    titleView;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2022-01-23
    • 2016-09-26
    • 2016-12-07
    • 2011-10-18
    • 2010-12-03
    相关资源
    最近更新 更多