Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change vertical position of back button text in UINavigationBar

I've been able to change the vertical posiiton of the back button icon but not the text.

I'm using the layoutSubviews method in 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 I make on UINavigationItemButtonView does not seem to have any effect, nor any frame change I make on it's UILabel subview that is the actual button text. When I log the views the frames seem to be changing but the text is not moving in my view. What am I doing wrong?

like image 636
Jack Rogers Avatar asked Sep 29 '22 13:09

Jack Rogers


1 Answers

You subclass a UINavigationBar called MyNavigationBar, in layoutSubviews, change the back indicator position.

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 backBarItem's title position by adding this in applicationDidFinished.

[[UIBarButtonItem appearanceWhenContainedIn:[MyNavigationBar class], nil] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, 8) forBarMetrics:UIBarMetricsDefault];
like image 147
gabbler Avatar answered Oct 09 '22 11:10

gabbler