[英]Issue with UINavigationBar title alignment
我在導航控制器中更改標題的字體時遇到問題。
這是我的代碼(來自我的UINavigationController的子類):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSDictionary * attributes = @{UITextAttributeTextColor :[UIColor whiteColor],
UITextAttributeFont :[UIFont fontWithName:@"Blanch" size:50],
};
self.navigationBar.titleTextAttributes = attributes;
CGFloat verticalOffset = -8;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
UIImage *image = [UIImage imageNamed:NAVBAR_IMAGE];
[self.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
看起來是這樣的:
如果我注釋掉設置字體的行,這就是它的樣子(與垂直偏移一樣):
垂直偏移存在的唯一原因是我認為它可能已經解決了問題。 我不需要它,現在已將其注釋掉,但是它不會更改輸出-在NavBar上它看起來還是一樣。
任何想法如何解決這個問題? 我認為自定義字體可能有很多行距,我想可能是問題所在。
我一直在試圖找到一種方法來更改標題的原點/高度/基線對齊,但是看不到任何一種方法來執行這些操作。 任何想法如何解決這個問題?
創建一個容器UIView
,該容器的寬度與標簽的寬度相同,並且與UINavigationBar
高度相同(約46像素?)。 將此容器視圖上的clipsToBounds
設置為YES。 現在,將標簽添加到此容器視圖,並使該容器視圖成為titleView。
設置ios5的Navigationbar圖像
if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"YOUR_NAVIGATION_BAR_IMAGE"] forBarMetrics:UIBarMetricsDefault];
}
設置ios6的Navigationbar圖像
if([[UINavigationBar appearance] respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"YOUR_NAVIGATION_BAR_IMAGE"] forBarMetrics:UIBarMetricsDefault];
然后,在標題上的導航欄中心創建標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.