Jack Rogers
Jack Rogers

Reputation: 305

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?

Upvotes: 5

Views: 2037

Answers (1)

gabbler
gabbler

Reputation: 13766

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];

Upvotes: 4

Related Questions