2011-10-15 83 views
2

是否有任何方法使用UIAppearance來更改UINavigationBar內部標籤的高度。以下是代碼和正在發生的事情的圖像,以便您瞭解問題所在。使用UIAppearance更改標籤高度

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-5.0 forBarMetrics:UIBarMetricsDefault]; 
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-5.0 forBarMetrics:UIBarMetricsLandscapePhone]; 

NSDictionary *textAttributes = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIFont fontWithName:@"MarketingScript" size:34.0], nil] 
                  forKeys:[NSArray arrayWithObjects:UITextAttributeFont, nil]]; 

enter image description here

回答

0

我結束了創建自定義的UIView解決這個:

+(NavigationBarTitleLabel *)titleLabelWithTitle:(NSString *)title { 
    // this will appear as the title in the navigation bar 
    NavigationBarTitleLabel *labelWrapper = [[NavigationBarTitleLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 34)]; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 8, 200, 32)]; 

    label.font   = [UIFont fontWithName:@"MarketingScript" size:28.0]; 
    label.text   = title; 
    label.textColor  = XHEXCOLOR(0XFFFFFF); 
    label.textAlignment = UITextAlignmentCenter; 
    label.backgroundColor = [UIColor clearColor]; 
    label.layer.shadowColor = [UIColor blackColor].CGColor; 
    label.layer.shadowRadius = 2; 
    [labelWrapper addSubview:label]; 
    [labelWrapper sizeToFit]; 

    return labelWrapper; 
} 

,然後設置在navigationItem於titleview屬性:

self.navigationItem.titleView = [NavigationBarTitleLabel titleLabelWithTitle:self.title]; 
+0

在這種情況下使用自定義視圖的優點是,如果您只是向其中添加UILabel?爲什麼不把它變成一個簡單的UIView? – Andrew

0

也許你可以得到UIAppearance的內部UINavigationBar的的UILabel。

+0

不,'UILabel' [不能樣式] (http://stackoverflow.com/q/11839044/141721)使用'UIAppearance'。 –