美文网首页ios
iOS11 导航栏 返回键完美设置

iOS11 导航栏 返回键完美设置

作者: Vinch | 来源:发表于2017-11-07 14:59 被阅读413次

    直接上代码了:
    写在父类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 导航栏 返回键完美设置

        本文链接:https://www.haomeiwen.com/subject/crljmxtx.html