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.
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.
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.
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.
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With