zoukankan      html  css  js  c++  java
  • iOS-隐藏Navigationbar【导航栏无缝圆滑的隐藏】

    1.ViewController

    .m

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.title = @"隐藏导航栏";
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor lightGrayColor];
        button.frame = CGRectMake(10, 100, 60, 30);
        [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        
        self.navigationController.delegate = self;
    }
    - (void)buttonClick{
        ///跳转到KKViewController
        [self performSegueWithIdentifier:@"pusht" sender:nil];
    }

    头部代理

    @interface ViewController ()<UINavigationControllerDelegate>

    代理方法

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
        
        [self.navigationController setNavigationBarHidden: [self hiddenBarVc: viewController] animated: animated];
    }
    
    - (BOOL)hiddenBarVc:(UIViewController *)viewController {
        
        BOOL needHideNaivgaionBar = NO;
        
        if ([viewController isKindOfClass: [KKViewController class]]) {
            needHideNaivgaionBar = YES;
        }
        
        return needHideNaivgaionBar;
    }

    2.KKViewController(目标ViewController)

    新建一个KKViewController

    .h

    @property (nonatomic,strong) id popDelegate;

    .m

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"第二个页面";
        [self popSet];
    }
    - (void)popSet{
        _popDelegate = self.navigationController.interactivePopGestureRecognizer.delegate;
        SEL action = NSSelectorFromString(@"handleNavigationTransition:");
        UIPanGestureRecognizer *popPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self.popDelegate action:action];
        popPanGesture.maximumNumberOfTouches = 1;
        popPanGesture.delegate = self;
        [self.view addGestureRecognizer: popPanGesture];
    }

    头部代理

    @interface KKViewController ()<UIGestureRecognizerDelegate>

    手势代理方法

    - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer{
        
        ///【下面两个方法写一个】
        ///全屏拖动
        CGPoint tragPoint = [gestureRecognizer translationInView:gestureRecognizer.view];
        if (tragPoint.x <= 0){
            return NO;
        }
        else{
            if (self.navigationController.viewControllers.count <= 1){
                return NO;
            }
            else{
                return YES;
            }
        }
        
        
    //    ///局部允许拖动
    //    CGPoint tragPoint = [gestureRecognizer locationInView:gestureRecognizer.view];
    //    NSLog(@"x=%f;y=%f",tragPoint.x,tragPoint.y);
    //    if (tragPoint.x > 60){///拖动的范围
    //        return NO;
    //    }
    //    else{
    //        if (self.navigationController.viewControllers.count <= 1) {
    //            return NO;
    //        }
    //        else{
    //            return YES;
    //        }
    //    }
    }

    效果图

    延伸

    最后再推荐一个Git开源,覆盖全屏pop手势 FDFullscreenPopGesture,它里面也实现了隐藏导航栏的功能,很流畅!

  • 相关阅读:
    事件类型
    事件
    节点样式
    节点
    将博客搬至CSDN
    ios开发 上传到App Store 时出错. iTunes Store Operation Failed, An Error occurred uploading to the iTunes store.
    ios开发百度高德地图经纬度相互转换的算法解析
    ios开发使用cocoapods倒入一堆的三方库之后开始崩溃了。发觉是导入极光引用的iOS10UserNotifications.framework导致的问题 Reason: image not found
    ios开发webview 的三种引用方式以及动态更新本地静态页的方法
    ios开发oc高仿京东金融白条额度余额的 ios开发水波纹 ios开发水正弦曲线波纹 ios开发雷达扫描的动画效果
  • 原文地址:https://www.cnblogs.com/wangkejia/p/7852934.html
Copyright © 2011-2022 走看看