2012-03-28 91 views
7

我沒有太多的運氣將垂直居中的標籤添加到UINavigationBar的TitleView上。你可以在下面看到它的樣子。在UINavigationBar中垂直居中標籤TitleView

Text not vertically aligned

這是我如何添加標籤:

UILabel *titleLabel = [[UILabel alloc] init]; 
titleLabel.text = NSLocalizedString(@"activeSessionsTitle",@""); 
titleLabel.font = [Util SETTING_NEO_HEADER_FONT]; 
titleLabel.textColor = [UIColor whiteColor]; 
titleLabel.backgroundColor = [UIColor clearColor]; 
titleLabel.textAlignment = UITextAlignmentCenter; 
titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f]; 
titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f); 
[titleLabel sizeToFit]; 

super.navigationItem.titleView = titleLabel; 

我覺得它爲什麼這樣做的原因是有一些奇怪的與我使用的是實際的字體 - 如我不得不在按鈕等內部重新定位。除了導航欄外,我已經能夠解決這個問題了。

+0

看到這個職位http://stackoverflow.com/a/8475788/716216 – 2012-03-28 22:08:58

回答

11

我最終使用的答案RPM的組合,左,上我的問題的意見之一。這是最終的固定對我來說:

UIView *customTitleView = [[UIView alloc] init]; 
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 3.0f, 200.0f, 30.0f)]; 
titleLabel.text = titleString; 
titleLabel.font = [Util SETTING_NEO_HEADER_FONT]; 
titleLabel.textColor = [UIColor whiteColor]; 
titleLabel.backgroundColor = [UIColor clearColor]; 
titleLabel.textAlignment = UITextAlignmentCenter; 
titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f]; 
titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f); 
[titleLabel sizeToFit]; 

customTitleView.frame = CGRectMake(self.navigationItem.titleView.frame.size.width/2 - titleLabel.frame.size.width/2, self.navigationItem.titleView.frame.size.height/2 - titleLabel.frame.size.height/2, titleLabel.frame.size.width, titleLabel.frame.size.height); 

[customTitleView addSubview:titleLabel]; 
[titleLabel release]; 

[self.navigationItem setTitleView:customTitleView]; 
[customTitleView release]; 

如果您在使用這個代碼塊,你可能有與標題標籤的initWithFrame的「Y」值發揮:打電話。我已將它設置爲3.0f,但您可能需要根據自己的用法對其進行調整。

其他解決方案似乎不適合我的原因是他們會水平偏離中心,這取決於我是否有一個barbutton(左或右)。如果沒有酒吧按鈕,那很好,如果有兩個,那就很好。但是會導致它偏離中心。

+0

適用於iOS的不到5,但是對於iOS 5以上的版本,你有外觀API,你不需要這樣做,所以請參閱@ Moe回答。 – bandejapaisa 2013-08-19 11:21:51

1

我不認爲這與你的字體有任何關係。我也使用過titleView,我認爲它以奇怪的方式展示視圖的方式並不考慮視圖大小。

你也可以設置標籤視圖的幀作爲這樣

titleLabel.frame = CGRectMake(self.navigationItem.titleView.frame.size.width/2 - titleLabel.frame.size.width/2, self.navigationItem.titleView.frame.size.height/2 - titleLabel.frame.size.height/2, titleLabel.frame.size.width, titleLabel.frame.size.height); 

self.navigationItem.titleView = titleLabel; 
+0

這似乎並沒有發揮作用 - 雖然我沒有使用你的答案以不同的方式! – Hosemeyer 2012-03-29 14:38:50

+0

啊是的 - 你需要將它添加到超級視圖,並將其分配爲標題視圖。我很抱歉 - 我應該早點告訴你的。我也知道這個:( – RPM 2012-04-03 22:06:55

+0

其實什麼是titleLabel? – 2012-11-09 07:42:30

28

打開一個老問題,但我發現這是非常有用的。

的iOS 5可以讓您使用[UINavigationBar appearance]這是偉大的改變標題欄的標題視圖,而無需自定義的UIView:

[[UINavigationBar appearance] setTitleTextAttributes: 
     [NSDictionary dictionaryWithObjectsAndKeys: 
      [UIColor redColor], 
      UITextAttributeTextColor, 
      [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
      UITextAttributeTextShadowColor, 
      [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
      UITextAttributeTextShadowOffset, 
      [UIFont fontWithName:@"HelveticaNeue" size:25.0f], 
      UITextAttributeFont, 
      nil]]; 

有時候,這取決於你所使用的標題視圖可以將UIFont垂直關閉。

爲了解決這個問題,你可以使用:

[self.navigationBar setTitleVerticalPositionAdjustment:(float) forBarMetrics:UIBarMetricsDefault]; 

(float)是推titleview的下一個正值,負值推titleview的了。

我希望這會有所幫助。

+7

您還可以使用UINavigationBar外觀設置垂直位置調整,即:[[[UINavigationBar appearance] setTitleVerticalPositionAdjustment :((float)forBarMetrics:UIBarMetricsDefault];' – 2013-02-19 10:35:25

+2

您剛剛救了我的脖子,先生!拿我的upvote! :) – nburk 2014-12-10 17:30:25

+0

不客氣,nburk。 :) – Moe 2014-12-11 13:58:59

0
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName:UIFont(name:"Exo2-Bold", size: 18) as! AnyObject,NSForegroundColorAttributeName:UIColor.whiteColor()] 
+1

不是OP要求的。 – Cesare 2017-03-04 15:20:24