Centering a vertical label in a UINavigationBar TitleView

I don't have much luck centering the shortcut vertically, which I add to the TitleView on the UINavigationBar . You can see how it looks below.

Text not vertically aligned

This is how I add a shortcut:

 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; 

I think the reason this does this is that there is something strange with the actual font that I use - since I had to move a lot inside the buttons, etc. I was able to solve the problem everywhere, but the navigation bar.

+7
source share
4 answers

I ended up using a combination of the RPM answer on the left and one of the comments on my question. This is what ultimately fixed this for me:

 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]; 

If you use this piece of code, you may have to play with the value "y" on the header label of initWithFrame: call. I installed it in 3.0f, but you may have to tweak it a bit for your own use.

The reason the other solutions did not seem to work for me is that they will exit the center horizontally if I have one barbaton (left or right). If there were no buttons, everything was in order, and if there were two, everything was in order. But you can make him leave the center.

+11
source

Opening an old question, but I found it very useful.

iOS 5 allows the use of [UINavigationBar appearance] , which is great for changing the title of the title bar without the need for a custom 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]]; 

Sometimes, depending on the UIFont used, the header view may be disabled vertically.

To fix this, you can use:

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

(float) is a positive value to push the title title down and a negative value to increase the title.

Hope this helps.

+28
source

I don’t think this has anything to do with your font. I also used the titleView, and I think it sets out the view in a strange way and does not take into account what the size of your view is.

You can also set the frame of your label view as such.

 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; 
+1
source
 UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName:UIFont(name:"Exo2-Bold", size: 18) as! AnyObject,NSForegroundColorAttributeName:UIColor.whiteColor()] 
0
source

All Articles