iOS11 导航栏 返回键完美设置

直接上代码了:
写在父类UINavigationController中的

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if (@available(iOS 11.0, *)) {
        //关键适配
        UIImage *backButtonImage = [[UIImage imageNamed:@"nav_arrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.navigationBar.backIndicatorImage = backButtonImage;
        self.navigationBar.backIndicatorTransitionMaskImage = backButtonImage;
        [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-kScreenWidth, 0)forBarMetrics:UIBarMetricsDefault];//UIOffsetMake(-kScreenWidth, 0)zh只要横向偏移,纵向偏移返回图标也会偏移
    }
    else{
        //设置返回按钮的图片
        UIImage *backButtonImage = [[UIImage imageNamed:@"nav_arrow"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
        [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        //将返回按钮的文字position设置不在屏幕上显示
        [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-kScreenWidth, -kScreenHeight)forBarMetrics:UIBarMetricsDefault];
    }
    if (self.childViewControllers.count > 0) {
        //哥么你要想隐藏.那么必须在我push之前给我设置
        viewController.hidesBottomBarWhenPushed = YES;
    }
    //调用父类的方法
    [super pushViewController:viewController animated:animated];
}

-(void)back
{
    [self popViewControllerAnimated:YES];
}

你可能感兴趣的:(iOS11 导航栏 返回键完美设置)