【发布时间】:2011-05-13 13:19:26
【问题描述】:
我有一个自定义的 UINavigationBar(不同的大小、背景等),它在自定义标题视图中。我使用这段代码来实现这一点:
UIView * mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
mainView.backgroundColor = [UIColor yellowColor];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,kScreenWidth,80)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UINavigationItem *currentItem = [[UINavigationItem alloc] init];
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont fontWithName:@"Helvetica-Bold" size: 30.0];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:self.title];
[label sizeToFit];
[currentItem setTitleView:label];
[label release];
[navBar pushNavigationItem:currentItem animated:NO];
[currentItem release];
[mainView addSubview:navBar];
[navBar release];
self.view = mainView;
[mainView release];
但是,我现在的问题是,即使正确添加了自定义标题视图,它也不会与导航栏垂直居中。我错过了什么?
谢谢!
【问题讨论】:
-
为了……不要对屏幕尺寸进行硬编码,Apple 自 iPhone SDK 首次发布以来一直在宣传这一点。使用
[UIScreen mainScreen].applicationFrame,它将适用于 iPhone/iPad 或任何未来的设备。在通话或网络共享时有或没有更大的状态栏。 -
感谢 PeyloW,从现在开始会牢记这一点!
-
不要使用 UIScreen 的框架,当您的应用在 iOS 9 上处于分屏模式时,它会给您错误的大小。请改用窗口的框架。
标签: iphone objective-c cocoa-touch