美文网首页
iOS 加载storyboard和统一设置导航控制器返回按钮,

iOS 加载storyboard和统一设置导航控制器返回按钮,

作者: eryuxinling | 来源:发表于2016-08-12 11:38 被阅读1117次
    // 1.加载storyboard
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"storyboard名称" bundle:nil];
    // 2.初始化箭头指向的控制器
    xxxxViewController *xxVC = [storyboard instantiateInitialViewController];
    
    // 系统手势代理
    @property (nonatomic, strong) id popGesture;
    
    self.popGesture = self.interactivePopGestureRecognizer.delegate;
    
    // 想统一设置返回按钮,滑动移除控制器
    // 清空手势代理就能实现滑动返回,iOS6不支持
    
    // 当是跟控制器,还原代理,如果是非跟控制器,清空代理
    self.delegate = self;
    
    // 当控制器显示完毕的时候调用
    - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
        if (self.viewControllers[0] == viewController) {
            // 根控制器
            // 还原代理
            self.interactivePopGestureRecognizer.delegate = self.popGesture;
        } else {
            // 非根控制器,清空代理
            self.interactivePopGestureRecognizer.delegate = nil;
        }
    }
    
    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 当非根控制器设置导航条左侧返回按钮
       if (self.viewControllers.count > 1) {
          // 设置导航条左侧返回按钮
          viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage ImageNamed:@"XXX"] style:0 target:self action:@selector(back)];
       }
    }
    - (void)back {
       [self popViewControllerAnimated:YES];
    }
    
    // 设置导航条前景色
    [bar setTintColor:[UIColor whiteColor]];
    
    // 获取到导航条按钮的标识
    UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedIn:self, nil];
    // 修改返回按钮标题的位置
    [item setBackButtonTitlePositonAdjustment:UIOffsetMake(0, -100) forBarMetrics:UIBarMetricsDefault];
    
    // 全屏滑动移除控制器
    // 1.先修改系统的手势,系统没有给我们提供属性
    UIScreenEdgePanGestureRecognizer *gest = self.interactivePopGestureRecognizer;
    
    // 2.自己添加手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleNavigationTransition:)];
    [self.view addGestureRecognizer:pan];
    
    // 却Target 系统的私有属性
    //  KVC  [gest valueForKeyPath:@""];
    // 不知道 Target 真实类型
    // OC runtime 机制 只能动态获取当前类的成员属性,不能获取其子类或者父类的属性<objc/runtime.h>
    // __unsafe_unretained Class 要获取哪个类的成员属性
    // unsigned int *outCount 获取Class 下面的所有成员属性的个数
    unsigned int outCount = 0;
    Ivar *ivars = class_copyIvarList([UIGestureRecognizer class], &outCount)
    for (int i = 0; i < outCount; i++) {
        // 获取成员属性的名字
        NSString *name = @(ivar_getName(ivars[i]));
        NSLog(@"%@", name);
    }
    // _targets
     NSArray *targets = [gest valueForKeyPath:@"_targets"];
     NSLog(@"%@", targets[0]);
     id target = [targets[0] valueForKeyPath:@"_target"];
    
     UIPanGestureRecognizer *pan1 =  [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan)];
     [self.view addGestureRecognizer:pan1];
     pan1.delegate = self;
    
    id target = self.interactivePopGestureRecognizer.delegate;
    
    // 2.自己添加手势
    // 禁止系统的手势
    self.interactivePopGestureRecognizer.enabled = NO;
    
     UIPanGestureRecognizer *pan =  [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
     [self.view addGestureRecognizer:pan];
    
    
    pan.delegate = self;
    
    // 当开始滑动的时候就会调用 如果返回YES,可以滑动
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
        // 当是根控制器不让移除(禁止),非根控制器允许移除控制器
        BOOL open = self.viewCintrollers.count > 1;
        return open;
    }
    

    相关文章

      网友评论

          本文标题: iOS 加载storyboard和统一设置导航控制器返回按钮,

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