本文介绍了UINavigationBar自定义标题位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义的UINavigationBar(不同的大小,背景等),在自定义标题视图内。我使用这个代码实现这个:
I have a custom made UINavigationBar (different size, background etc) that has inside a custom title view. I used this code to achieve this:
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];
然而,我的问题是,即使自定义标题视图被正确添加,以NavBar为中心。
However, my problem now is that, even if the custom title view is correctly added, it's not vertically centered with the NavBar. What I am missing?
谢谢!
推荐答案
setTitleVerticalPositionAdjustment:forBarMetrics:
在改变标题位置,它工作在正常的标题如:
You can use setTitleVerticalPositionAdjustment:forBarMetrics:
in IOS 5 to change the title position, it works on normal title e.g.:
CGFloat verticalOffset = -4;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
这篇关于UINavigationBar自定义标题位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!