美文网首页
重写一个简单的导航控制器,可侧滑返回上一层

重写一个简单的导航控制器,可侧滑返回上一层

作者: 开局四带二 | 来源:发表于2018-08-08 18:19 被阅读0次

    #import "JHNav.h"

    @interface JHNav ()

    @end

    @implementation JHNav

    - (void)viewDidLoad {

        [super viewDidLoad];

        __weakJHNav*weakSelf =self;

        if([selfrespondsToSelector:@selector(interactivePopGestureRecognizer)])

        {

            self.interactivePopGestureRecognizer.delegate = weakSelf;

            self.delegate= weakSelf;

        }

        if(@available(iOS11.0, *)) {

            UIImage *backButtonImage = [[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] resizableImageWithCapInsets:UIEdgeInsetsMake(0,16,0, 0)];

            self.navigationBar.backIndicatorImage = backButtonImage;

            self.navigationBar.backIndicatorTransitionMaskImage = backButtonImage;

            [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateNormal];//将title 文字的颜色改为透明

            [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateHighlighted];//将title 高亮文字的颜色改为透明

        }else{

            [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0.0f,-60.0f) forBarMetrics:UIBarMetricsDefault];

            [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0.0f,-60.0f) forBarMetrics:UIBarMetricsCompact];

            //导航栏 返回键

            UIImage *backButtonImage = [[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] resizableImageWithCapInsets:UIEdgeInsetsMake(0,16,0, 0)];

            [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

            [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsCompact];

            [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0.0f,-60.0f) forBarMetrics:UIBarMetricsDefault];//导航栏 返回键文字位置

        }

    }

    - (id)initWithRootViewController:(UIViewController*)rootViewController

    {

        self= [superinitWithRootViewController:rootViewController];

        if(self) {

            // Custom initialization

        }

        return self;

    }

    - (void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated

    {

        if( [selfrespondsToSelector:@selector(interactivePopGestureRecognizer)] && animated ==YES)

        {

            self.interactivePopGestureRecognizer.enabled = NO;

        }

        [superpushViewController:viewControlleranimated:animated];

    }

    - (NSArray*)popToRootViewControllerAnimated:(BOOL)animated

    {

        if( [selfrespondsToSelector:@selector(interactivePopGestureRecognizer)] && animated ==YES)

        {

            self.interactivePopGestureRecognizer.enabled = NO;

        }

        return  [super popToRootViewControllerAnimated:animated];

    }

    - (NSArray*)popToViewController:(UIViewController*)viewController animated:(BOOL)animated

    {

        if( [selfrespondsToSelector:@selector(interactivePopGestureRecognizer)] )

        {

            self.interactivePopGestureRecognizer.enabled = NO;

        }

        return[superpopToViewController:viewControlleranimated:animated];

    }

    #pragma mark UINavigationControllerDelegate

    - (void)navigationController:(UINavigationController*)navigationController

           didShowViewController:(UIViewController*)viewController

                        animated:(BOOL)animate

    {

        if([selfrespondsToSelector:@selector(interactivePopGestureRecognizer)])

        {

            self.interactivePopGestureRecognizer.enabled = YES;

        }

    }

    -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer

    {

        if ( gestureRecognizer == self.interactivePopGestureRecognizer )

        {

            if ( self.viewControllers.count < 2 || self.visibleViewController == [self.viewControllers objectAtIndex:0] )

            {

                returnNO;

            }

        }

        return YES;

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    /*

    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */

    @end

    相关文章

      网友评论

          本文标题:重写一个简单的导航控制器,可侧滑返回上一层

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