【问题标题】:Navigation bar title is not centred in iPhone's portrait mode导航栏标题不在 iPhone 的纵向模式中居中
【发布时间】:2015-07-31 02:45:15
【问题描述】:

我使用大小为 wAny 和 hAny 的 Xib 文件创建了一个 ViewController,并且我使用了自动布局和约束来适应 UI 所有设备和所有方向。

一切正常,但是当我在 iPhone 上运行应用程序时,导航栏的纵向模式标题不居中,而将其旋转到横向模式时,标题居中。

我使用以下代码来设置标题

self.title = @"Download Schedule";

已经尝试过: 我试图将较小字体大小的自定义 UILabel 设置为导航栏的标题视图,但我仍然将它放在左侧。

这是我的右键代码

 //add right bar button for more options
    UIView *navBarButtonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    UIBarButtonItem *item0 = [UIBarButtonItem negativeSpacerWithWidth:5];
    UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [moreButton setBackgroundImage:[UIImage imageNamed:@"ic_web"] forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(addNewSchedule) forControlEvents:UIControlEventTouchDown];
    [navBarButtonsView addSubview:moreButton];

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:navBarButtonsView];
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:item0, backButton, nil];

【问题讨论】:

    标签: ios objective-c iphone uinavigationbar uinavigationitem


    【解决方案1】:

    还有其他事情正在发生 - 你没有告诉我们的事情。我怀疑您设置rightButtonItems 的方式有问题,以至于有不可见的按钮占用了右侧的空间。我只能通过故意使右侧按钮变宽来重现此问题:

    所以我怀疑你对右键应用了很大的宽度,或者有额外的不可见的右键项目占用了右侧的空间。

    【讨论】:

    • 很好,但这只是证明了我的观点。注释掉那个右键项目代码,你会看到标题的问题消失了——从而证明我是对的——是你的右键项目导致了问题。
    • 完美!!!!!!是的,您的观点是正确的,这是导致问题的正确按钮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 2020-12-29
    • 2013-08-22
    • 2011-02-18
    • 2012-02-22
    • 1970-01-01
    • 2011-01-09
    相关资源
    最近更新 更多