-->

iOS - UIBarButtonItem - Back Button title vertical

2020-07-01 18:46发布

问题:

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

回答1:

I had this issue too, I fixed it by using:

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, 1.5) forBarMetrics:UIBarMetricsDefault];