【发布时间】:2015-01-10 06:02:54
【问题描述】:
我已将自定义视图设置为导航栏 titleView,当页面是第一个视图时,控制器标题正确显示在中心
但是当视图控制器从另一个视图控制器被推送时,标题会向右移动
代码:
-(void)setUpTwoLineNavigationTitle {
CGFloat width = 0.95 * self.view.frame.size.width;
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
contentView.backgroundColor = [UIColor clearColor];
CGRect titleRect = contentView.frame;
titleRect.origin.y = 4;
titleRect.size.height = 20;
UILabel *titleView = [[UILabel alloc] initWithFrame:titleRect];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont systemFontOfSize:14.0];
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor blackColor];
titleView.text = @"";
[contentView addSubview:titleView];
CGRect subTitleRect = contentView.frame;
subTitleRect.origin.y = 24;
subTitleRect.size.height = subTitleRect.size.height - 24;
//CGRect subtitleFrame = CGRectMake(0, 24, 220, 44-24);
UILabel *subtitleView = [[UILabel alloc] initWithFrame:subTitleRect];
subtitleView.backgroundColor = [UIColor clearColor];
subtitleView.font = [UIFont systemFontOfSize:11.0];
subtitleView.textAlignment = NSTextAlignmentCenter;
subtitleView.textColor = [UIColor blackColor];
subtitleView.text = @"";
[contentView addSubview:subtitleView];
contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.navigationItem.titleView = contentView;
}
谁能帮我纠正这个问题?
【问题讨论】:
-
你的 contentView 太宽,所以它被后退按钮推到了右边。你需要它是视图宽度的 0.95 倍吗?在宽度的 0.6 倍处,它看起来会居中。
-
@rdelmar 谢谢,但如果我这样做,那么最初它看起来像左对齐,当子视图加载然后转移到中心
-
我在您的测试应用程序中看不到这种情况。在任何情况下,您都需要缩小视图以使其居中。您可能还需要做其他事情才能摆脱您提到的问题。
-
在这里查看我的答案:stackoverflow.com/a/33365965/4768700
标签: ios objective-c ios7 uinavigationbar