2010-06-26 100 views
14

我正在使用UINavigationItem的titleView屬性來設置具有所需字體大小/顏色的自定義UILabel。這是我的代碼:UINavigationItem titleView位置

self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 44.0)]; 
self.headerLabel.textAlignment = UITextAlignmentCenter; 
self.headerLabel.backgroundColor = [UIColor clearColor]; 
self.headerLabel.font = [UIFont boldSystemFontOfSize:20.0]; 
self.headerLabel.textColor = [UIColor colorWithRed:0.259 green:0.280 blue:0.312 alpha:1.0]; 
self.navigationItem.titleView = self.headerLabel; 

在導航欄中我還有一個左欄按鈕。結果是這樣的:

alt text http://cl.ly/41164eec656c96e7c913/content

正如你可以看到,文本不正確居中。我試過設置標籤的x原點,但這沒有效果。

+0

我看不到圖片... – turingtested 2016-12-13 15:35:49

回答

15

如果您將headerLabel作爲titleView的子視圖,則可以設置headerLabel的框架來控制它在titleView中的位置。

你現在這樣做的方式,你沒有這種控制。我認爲OS根據可用空間爲你選擇titleView的框架。

希望這會有所幫助!

+0

嘿,我試過這個,但是當我有左或右的按鈕時,我的titleView會稍微下壓。你能防止這種情況發生嗎? – quantumpotato 2010-09-17 16:12:22

+0

我的titleView甚至沒有顯示這樣做... – sebrock 2010-12-13 09:35:26

+0

這也不適用於我,或者我在這裏做錯了什麼?當我做[self.navigationItem.titleView addSubview:headerLabel];時,headerLabel完全不顯示。我是否需要首先將titleView設置爲某種東西? – 2011-03-01 13:16:10

1

只是計算需要精確的幀大小和左對齊:

UIFont* font = [UIFont fontWithName:@"Bitsumishi" size:20]; 
CGSize maximumLabelSize = CGSizeMake(296,9999); 
CGSize expectedLabelSize = [title sizeWithFont:font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeCharacterWrap]; 
CGRect frame = CGRectMake(0, 0, expectedLabelSize.width, expectedLabelSize.height); 
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease]; 
label.font = font; 
label.textAlignment = UITextAlignmentLeft; 
label.text = title; 
self.titleView = label; 
0
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; 
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; 
lbl.text = @"Home"; 
lbl.textAlignment = UITextAlignmentCenter; 
lbl.backgroundColor = [UIColor clearColor]; 
lbl.textColor = [UIColor whiteColor]; 
lbl.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20]; 
lbl.shadowColor = [UIColor blackColor]; 
lbl.shadowOffset = CGSizeMake(0,1); 
self.navigationItem.titleView = vw; 
[self.navigationItem.titleView addSubview:lbl]; 
19

initWithFrame代替只需使用init並把[self.headerLabel sizeToFit]你的代碼的最後一行。

+2

這就像一個魅力。沒有人應該對此投下評論。 – 2012-04-07 13:22:10

+0

我沒有投票,但我可以看到有人可能會爭辯說,它不回答問題,但提供了一個有用的切線技巧。 – Olie 2013-10-22 22:44:54

3

我已經在應用商店中的每個應用中爲我的導航欄使用了自定義標題標籤。我已經測試了很多不同的方式,並且通過在導航欄中使用自定義標籤的最簡單方法是完全忽略titleView並將標籤直接插入navigationController.view

使用此方法,即使您使用非標準尺寸的自定義navBar,標題標籤的框架始終與導航條的框架相匹配也很容易。

- (void)viewDidLoad { 
    [self.navigationController.view addSubview:self.titleLabel]; 
    [super viewDidLoad]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation: 
      (UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

- (void)didRotateFromInterfaceOrientation: 
      (UIInterfaceOrientation)fromInterfaceOrientation { 
    [self frameTitleLabel]; 
} 

- (UILabel *) titleLabel { 
    if (!titleLabel) { 
     titleLabel = [[UILabel alloc]   
      initWithFrame:self.navigationController.navigationBar.frame]; 

     titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18]; 
     titleLabel.text = NSLocalizedString(@"Custom Title", nil); 
     titleLabel.textAlignment = UITextAlignmentCenter; 
     titleLabel.lineBreakMode = UILineBreakModeTailTruncation; 
    } 
    return titleLabel; 
} 

- (void) frameTitleLabel { 
    self.titleLabel.frame = self.navigationController.navigationBar.frame; 
} 

一個需要注意這種方法是,你的標題可以流過你的導航欄有任何頂部按鈕,如果你不小心,並設置標題太長。但是,國際海事組織,這是很少處理問題比1)標題不正確,當你有一個rightBarButton或2)標題沒有出現,如果你有一個leftBarButton標題。

+1

感謝您的評論。 請注意,當我實現您的方法時,按下後退按鈕時創建的視圖不會消失。 – 2013-11-25 23:01:55

3

我有同樣的問題;我只是通過計算標題長度並相應地設置標籤框架寬度來解決此問題。雖然這不是一個完美的,但可以管理。這是代碼。

label = [[UILabel alloc] init]; 
label.backgroundColor = [UIColor clearColor]; 
label.font = [ UIFont fontWithName: @"XXII DIRTY-ARMY" size: 32.0 ]; 
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.0f]; 
label.textAlignment = UITextAlignmentCenter; 
label.textColor =[UIColor orangeColor]; 
//label.text=categoryTitle; 
CGFloat verticalOffset = 2; 
NSString *reqSysVer = @"5.0"; 
NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; 
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)   
{ 
    if (categoryTitle.length > 8) 
    { 
    label.frame = CGRectMake(0, 0, 300, 44); 
    }else { 
     label.frame = CGRectMake(0, 0, 80, 44); 
    } 

    self.navigationItem.titleView = label; 
    self.navigationItem.title=label.text; 
    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault]; 
    [[UIBarButtonItem appearance] setTintColor:[UIColor newBrownLight]]; 


} 
0

對我來說有效的是更新viewDidAppear方法中的titleView框架。

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    UIView *titleView = self.navigationItem.titleView; 
    CGRect navBarFrame = self.navigationController.navigationBar.frame; 
    [titleView setFrame:CGRectMake((CGRectGetWidth(navBarFrame) - TitleWidth)/2, (CGRectGetHeight(navBarFrame) - TitleHeight)/2, TitleWidth, TitleHeight)]; 
}