查看: 579|回复: 0

[IOS开发教程] iOS 导航栏无缝圆滑的隐藏 Navigationbar实例代码

发表于 2017-12-27 08:00:00

1.ViewController

.m

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. self.title = @"隐藏导航栏";
  4. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  5. button.backgroundColor = [UIColor lightGrayColor];
  6. button.frame = CGRectMake(10, 100, 60, 30);
  7. [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
  8. [self.view addSubview:button];
  9. self.navigationController.delegate = self;
  10. }
  11. - (void)buttonClick{
  12. ///跳转到KKViewController
  13. [self performSegueWithIdentifier:@"pusht" sender:nil];
  14. }
复制代码

头部代理

  1. @interface ViewController ()<UINavigationControllerDelegate>
复制代码

代理方法

  1. - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  2. [self.navigationController setNavigationBarHidden: [self hiddenBarVc: viewController] animated: animated];
  3. }
  4. - (BOOL)hiddenBarVc:(UIViewController *)viewController {
  5. BOOL needHideNaivgaionBar = NO;
  6. if ([viewController isKindOfClass: [KKViewController class]]) {
  7. needHideNaivgaionBar = YES;
  8. }
  9. return needHideNaivgaionBar;
  10. }
复制代码

2.KKViewController(目标ViewController)

新建一个KKViewController

.h

  1. @property (nonatomic,strong) id popDelegate;
复制代码

.m

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. self.title = @"第二个页面";
  4. [self popSet];
  5. }
  6. - (void)popSet{
  7. _popDelegate = self.navigationController.interactivePopGestureRecognizer.delegate;
  8. SEL action = NSSelectorFromString(@"handleNavigationTransition:");
  9. UIPanGestureRecognizer *popPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self.popDelegate action:action];
  10. popPanGesture.maximumNumberOfTouches = 1;
  11. popPanGesture.delegate = self;
  12. [self.view addGestureRecognizer: popPanGesture];
  13. }
复制代码

头部代理

  1. @interface KKViewController ()<UIGestureRecognizerDelegate>
复制代码

手势代理方法

  1. - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer{
  2. ///【下面两个方法写一个】
  3. ///全屏拖动
  4. CGPoint tragPoint = [gestureRecognizer translationInView:gestureRecognizer.view];
  5. if (tragPoint.x <= 0){
  6. return NO;
  7. }
  8. else{
  9. if (self.navigationController.viewControllers.count <= 1){
  10. return NO;
  11. }
  12. else{
  13. return YES;
  14. }
  15. }
  16. // ///局部允许拖动
  17. // CGPoint tragPoint = [gestureRecognizer locationInView:gestureRecognizer.view];
  18. // NSLog(@"x=%f;y=%f",tragPoint.x,tragPoint.y);
  19. // if (tragPoint.x > 60){///拖动的范围
  20. // return NO;
  21. // }
  22. // else{
  23. // if (self.navigationController.viewControllers.count <= 1) {
  24. // return NO;
  25. // }
  26. // else{
  27. // return YES;
  28. // }
  29. // }
  30. }
复制代码

效果图

总结

以上所述是小编给大家介绍的iOS 导航栏无缝圆滑的隐藏 Navigationbar,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对程序员之家网站的支持!



回复

使用道具 举报