Change vertical position of button text in UINavigationBar

I was able to change the vertical position of the back button icon, but not the text.

I use the layoutSubviews method in a UINavigationBar:

- (void)layoutSubviews { [super layoutSubviews]; BOOL fixed = NO; NSArray *classNamesToReposition = @[@"_UINavigationBarBackIndicatorView", @"UINavigationButton", @"UINavigationItemButtonView"]; for (UIView *view in [self subviews]) { if ([classNamesToReposition containsObject:NSStringFromClass([view class])] && !fixed) { CGRect frame = [view frame]; if ([NSStringFromClass([view class]) isEqualToString:@"_UINavigationBarBackIndicatorView"]) { frame.origin.y = 14.5; } else if ([NSStringFromClass([view class]) isEqualToString:@"UINavigationButton"]) { frame.origin.y = 9.0; } else if ([NSStringFromClass([view class]) isEqualToString:@"UINavigationItemButtonView"]) { frame.origin.y = 5.0; } [view setFrame:frame]; } } } 

The problem is that any frame change that I make in the UINavigationItemButtonView does not seem to have any effect, nor any frame change that I make on it. The true representation of UILabel is the actual text of the button. When I register views, frames seem to change, but the text does not move in my view. What am I doing wrong?

+5
source share
1 answer

You subclass the UINavigationBar called MyNavigationBar, in layoutSubviews, reposition the indicator back.

 for (UIView *view in [self subviews]) { CGRect frame = [view frame]; if ([NSStringFromClass([view class]) isEqualToString:@"_UINavigationBarBackIndicatorView"]) { frame.origin.y = 19.5; //default is 11.5, move down by 8. } [view setFrame:frame]; } 

And you can change the position of the backBarItem header by adding this to applicationDidFinished.

 [[UIBarButtonItem appearanceWhenContainedIn:[MyNavigationBar class], nil] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, 8) forBarMetrics:UIBarMetricsDefault]; 
+4
source

Source: https://habr.com/ru/post/1205519/


All Articles