Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Bar Back Button, text not center alligined

I am customizing the back button in the navigation bar using appearance proxy, also setting image and text attributes. Everything works fine but the text in back button is not center aligned.

Here is my code.

UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
 UIImage *buttonBack32 = [[UIImage imageNamed:@"NavigationBackButton"] 
                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 5)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack32 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
 [[UIBarButtonItem appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
        [UIColor whiteColor], 
        UITextAttributeTextColor, 
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
        UITextAttributeTextShadowColor, 
        [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
        UITextAttributeTextShadowOffset,
  [UIFont fontWithName:@"xxx" size:16.0], 
  UITextAttributeFont,
  nil] 
                                            forState:UIControlStateNormal]; 
self.navigationItem.backBarButtonItem = backButton;

If I set the font size "0.0" then text becomes too small in size and still not centered aligned.

Thanks in advance for any help.

like image 360
fibnochi Avatar asked Jun 04 '12 07:06

fibnochi


People also ask

How do I center align items in The navbar?

Aligning items to left, right, and center within the Navbar The items within the navbar can be aligned using margin utilities. The.mx-auto class can be used to align the items to the center of the navbar. The.ms-auto class is used to align items to the right of the navbar.

What is the default alignment of links in CSS?

Centered link --> <!-- Left-aligned links (default) --> <!-- Right-aligned links --> Tip: To create mobile-friendly, responsive navigation bars, read our How To - Responsive Top Navigation tutorial. Tip: Go to our CSS Navbar Tutorial to learn more about navigation bars.

What does the title of the back button mean?

The title of the back button gives a clue to the previous view controller that is pushing the current view, so the title is coming from the former view controller. This is the key concept when you are trying to edit the back button title. The back button title is coming from the previous view controller.

How to make the back button title empty?

You can also set a title with navigationItem.backButtonTitle. But to make an empty back title, you have to set backButtonTitle to "" not nil. Set backButtonTitle to nil would use the navigation title as a back button title. There is one behavior different that you should know when overriding the back button title.


1 Answers

You can use the setTitlePositionAdjustment:forBarMetrics: method to set the title offset as required. For example:

[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0f, 5.0f) forBarMetrics:UIBarMetricsDefault];
like image 105
Chris Gummer Avatar answered Sep 21 '22 03:09

Chris Gummer