美文网首页
iOS- 12处理导航控制器业务逻辑

iOS- 12处理导航控制器业务逻辑

作者: 麦穗0615 | 来源:发表于2016-08-23 23:47 被阅读100次

    如图所示:

    跳转逻辑图示

    跳转逻辑
    左边控制器
    代码示例:
    #pragma mark- 设置就会调用
    - (void)setting:(UIButton *)btn{
    /*
     1.   底部条没有隐藏
     2.   处理返回按钮的样式:2.1去设置控制器去设置
     */
    
    //   跳转到设置界面
    ZYSettingTabViewController *settingVc = [[ZYSettingTabViewController alloc]init];
    //   当push的时候隐藏tabBar
     //  必须在跳转之前去设置
    settingVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:settingVc animated:YES];
    
    }
    

    右边控制器
    #import "ZYSettingTabViewController.h"

     @interface ZYSettingTabViewController ()
    
     @end
    
     @implementation ZYSettingTabViewController
    
      - (void)viewDidLoad {
         [super viewDidLoad]
    
       self.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithNorImage:@"navigationButtonReturn"
                                                                           highImage:@"navigationButtonReturnClick" 
                                                                              target:self  
                                                                              action:@selector(back) 
                                                                               title:@"返回"];
    
     }
     #pragma mark- 返回按钮
      - (void)back
     {
    
         [self.navigationController popViewControllerAnimated:YES];
     }
    

    对返回按钮的封装

    + (instancetype)backItemWithNorImage:(NSString *)norImage
                           highImage:(NSString *)highImage
                              target:(id)target
                              action:(SEL)action
                               title:(NSString *)title
       {
    
         //  设置导航条左边按钮
         UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
         //  文字
        [backButton setTitle:title forState:UIControlStateNormal];
         //  图片
      [backButton setImage:[UIImage imageNamed:norImage] forState:UIControlStateNormal];
      [backButton setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
         //   文字颜色
      [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      [backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
      //   根据内容适配尺寸
      [backButton sizeToFit];
      //   设置左边按钮离边距的距离
      backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
      //   添加点击事件
      [backButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
      //   设置左边按钮
      return  [[UIBarButtonItem alloc]initWithCustomView:backButton];
    
      }
    

    设置全局返回按钮

    • 我们发现只有在push的时候,改变栈顶控制器的返回按钮,即可。
      重写push方法,进行拦截。
      /*重写push方法进行拦截--设置全局返回按钮/
      - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
      {
       if (self.childViewControllers.count > 0) {//  非根控制器
        //  设置返回按钮,只有非根控制器,才有返回按钮(tabBar有几个子控制器)
       viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithNorImage:@"navigationButtonReturn"highImage:@"navigationButtonReturnClick" target:self action:@selector(back) title:@"返回"];
          }
    
        //   真正的跳转
          [super pushViewController:viewController  animated:animated];
          }
      #pragma mark- 点击事件
       - (void)back
        {
            [self popViewControllerAnimated:YES];
       }

    相关文章

      网友评论

          本文标题:iOS- 12处理导航控制器业务逻辑

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