iOS - UIBarButtonItem - Back Button title vertical position not adjusting - ios

I am setting a custom font in my UIBarButtonItem objects using UIAppearance. This works fine and sets the font correctly. However, I do need to adjust the button title's vertical position to cater for the sizing of the new font.
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIFont fontWithName:#"Sketch Rockwell" size:12] forKey:UITextAttributeFont] forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, 1.5) forBarMetrics:UIBarMetricsDefault];
The problem is that the title's vertical position is adjusted correctly in a regular UIBarButtonItem, but the back buttons on the navigation bar do not get adjusted. I presume that I am targeting the correct object in my code because its font gets updated, just not its title's vertical position.
Does anyone have an idea of how to target the back buttons directly?
Thanks
Brian

I had this issue too, I fixed it by using:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, 1.5) forBarMetrics:UIBarMetricsDefault];

Related

iOS right bar button background image overlap with title on ios11

I am facing issue in margin of right bar button item title.
From iOS11 background image is overlapped means it is not properly right aligned.
It was working pretty good till iOS 10.
Code snippet:
UIBarButtonItem *rightItem = self.navigationItem.rightBarButtonItem;
[rightItem setBackgroundImage:[UIImage imageNamed:IMAGE_NAVIGATION_NEXT_BUTTON_DISABLED_iOS7]
forState:UIControlStateDisabled
barMetrics:UIBarMetricsDefault];
[rightItem setBackgroundImage:[UIImage imageNamed:IMAGE_NAVIGATION_NEXT_BUTTON_iOS7]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
rightItem.title = #"Next";
Before iOS11 it was working like this

UINavigationBar Back button duplicates twice

I have used the following code in my appDelegate and customizing back bar button goes well. When i used segue for the controllers navigation backbarbutton appears twice.
UIImage *buttonPortait = [[UIImage imageNamed:#"back-icon"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0,0, 0,0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonPortait
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[UIBarButtonItem.appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -64) forBarMetrics:UIBarMetricsDefault];
I also used navigationBar setTranslucent:NO but yet the problem surviving. Any solution for this problem. Thanks in advance.
I got the solution by altering the image size for the back bar button.
I used image size with height 30x(non-retina)/60x(retina) and width 90x(non-retina)/180x(retina)
cheers!!!

Center navigation bar title vertically

I have a simple storyboard containing a navigation controller with a static table view controller. I have added a view controller and connected it to one of the cells via a push segue. One thing I have noticed is that the navigation bar title doesn't center properly vertically with the back button and UIBarButtonItem.
Here is an image of what it looks now.
Notice that it is all aligned to the bottom. What I want is, is a way to either move the title down or move the buttons up. If the buttons were moved up, the back button arrow should be moved as well, and allow to return to the main table view.
Here is the code I'm using to set the UIBarButtonItem font in the view controller (If it helps) :
[clearButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"ChalkboardSE-Regular" size:15], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
And here is what I'm using to set the font for the back button in the app delegate, didFinishLaunchingWithOptions::
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:#"ChalkboardSE-Regular" size:15]}forState:UIControlStateNormal];
You can move the title down using: [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:(float) forBarMetrics:UIBarMetricsDefault];
SWIFT version for iOS 8+
self.navigationController!.navigationBar.setTitleVerticalPositionAdjustment(adj, forBarMetrics: .Default)
self is a UIViewController:
Note: Consider specifying adjustments for the other bar metrics options, to refine the offset for landscape mode and compact devices, because one offset may not look right on all phones in all positions.
To move title down :
self.navigationController!.navigationBar.setTitleVerticalPositionAdjustmen(+Value, forBarMetrics: .Default)
To move title up:
self.navigationController!.navigationBar.setTitleVerticalPositionAdjustmen(-Value, forBarMetrics: .Default)
In swift 5:
self.navigationBar.setTitleVerticalPositionAdjustment(ajustment, for:.default)

Remove UIBarButtonItem image

I have a UIBarButtonItem that I originally change the background image to. But I would like to be able to change the same UIBarButtonItem back to a default looking one (specifically a done button). Then again back to the way it was prior to (that shouldn't be a problem).
Here is how I change the appearance at first:
[menuButton setBackButtonBackgroundImage:[UIImage imageNamed:#"menuButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Then back I was trying things like:
menuButton.style = UIBarButtonItemStyleDone;
menuButton.title = #"Done";
//the above didn't do anything, so I tried to make my own image to
//replace the first image. But below did't do anything either.
[menuButton setBackButtonBackgroundImage:[UIImage imageNamed:#"doneButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Use setBackgroundImage:... instead of setBackButtonBackgroundImage:..., you're only setting the appearance of back buttons (in navigation bars).

Move a UIBarButton Item Down A Couple Pixels

I have a pop up window that slides on screen when a certain button is pressed.
This view has a custom look apart from the rest of the app. It has an .xib file as well. I have managed to change the header background color by adding a customClass to the toolbar in IB. I have managed to change the text style of the title text... It took me some time to figure that out. What I had to do was make an IBOutlet from the title item in IB, then apply the changes in my m...like so
- (void)customizeAppearance
{
//Sets custom text attribures for this views headers text
[_customNavText setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor, //sets text color
[UIColor blackColor], UITextAttributeTextShadowColor, //sets text shadow color
[NSValue valueWithUIOffset:UIOffsetMake(0, 3)], UITextAttributeTextShadowOffset, //Moves drop shadow by its coordinates
[UIFont fontWithName:#"Helvetica-Bold" size:27.0], UITextAttributeFont,nil] //sets text font and size
forState:UIControlStateNormal];
}
and this function gets called once the view loads. So my text has its own style on this view only. Great.
So, maybe I am getting to picky, but now I want to move the text down a couple pixels.
This is what I did, I added the following code inside the customizeApperance function...
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(25.0f, 25.0f) forBarMetrics:UIBarMetricsDefault];
But this does something interesting, it does not move my text down for the UIBarButtonItem I have just applied those styles too, instead it moves the text of the buttons on the nav bar, such as the cancel button.
So how do I move the UIBarButtonItem thats in the middle, containing text for the navigation header, rather than the text of the buttons on the side?
I think you might be looking for:
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:1 forBarMetrics:UIBarMetricsDefault];

Resources