【问题标题】:How to hide the UINavigationBar but still have the swipe left to go back gesture automatically?如何隐藏 UINavigationBar 但仍然向左滑动以自动返回手势?
【发布时间】:2015-03-22 07:57:18
【问题描述】:

我使用的是笔尖,而不是情节提要。无论如何,我正在从一个视图控制器推送到下一个:

[self.navigationController pushViewController:vc animated:YES];

但在下一个视图控制器“vc”上,我希望隐藏导航栏。但是,我仍然希望能够向左滑动以转到上一个屏幕,以及能够放置一个后退按钮来让用户返回。

但是,当我在下一个视图控制器中执行 [self.navigationController setNavigationBarHidden:YES animated:animated]; 时,这会隐藏导航栏,但您不能再滑动导航回来。

我怎样才能拥有这种向后滑动的功能,并将后退按钮放在屏幕上(只是没有导航栏?)

我正在尝试模仿 Yahoo News Digest 的行为 - 请注意,它有导航按钮,然后滑动返回,但没有导航栏。此外,当您向下滚动时,后退按钮会消失。

我尝试过让导航栏变成半透明,但随后栏会阻止触摸,因此用户无法点击栏下方的内容。有任何想法吗?谢谢!

【问题讨论】:

标签: ios objective-c uinavigationcontroller uinavigationbar


【解决方案1】:

斯威夫特

您可以使用此方法隐藏导航栏:

self.navigationController?.navigationBar.isHidden = true
// or
self.navigationController?.setNavigationBarHidden(true, animated: true)

启用 interactivePopGestureRecognizer documentation

self.navigationController?.interactivePopGestureRecognizer?.delegate = self as! UIGestureRecognizerDelegate

// Enable gesture to pop the top view controller off the navigation stack
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true

// Make an extension for your View Controller
extension MyViewController: UIGestureRecognizerDelegate {}

或

添加 EdgePanGestureRecognizer 以识别左滑然后返回:

// in viewDidLoad
let edgePan = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(leftEdgeSwipe))
edgePan.edges = .left

view.addGestureRecognizer(edgePan)

// Method to go back
@objc func leftEdgeSwipe(_ recognizer: UIScreenEdgePanGestureRecognizer) {
   if recognizer.state == .recognized {
      self.navigationController?.popViewController(animated: true)
   }
}

【讨论】:

    【解决方案2】:

    你可以做到这很简单,只需隐藏你的导航栏

    [self.navigationController setNavigationBarHidden:YES animated:animated];
    

    并添加滑动手势以导航回上一视图

    - (void)viewDidLoad
    {
       [super viewDidLoad];
    
     //Add Swipe Gesture to navigate back to view
    UISwipeGestureRecognizer *backtoview = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler)];
    [self.view addGestureRecognizer: backtoview];
    }
    
    
    -(void)swipeHandler{
       [self.navigationController popViewControllerAnimated:YES];
    }
    

    如果你想要方向,你可以添加方向来滑动手势,如果你想要按钮事件,那么添加自定义按钮并将相同的方法添加到按钮操作

    这对你有帮助!

    【讨论】:

    • 但这不是交互式滑动:(并且没有UIViewControllerTransitionCoordinator参与;'-(
    • 没错,UISwipeGestureRecognizer 并不理想,因为我希望保留本机交互式滑动行为
    【解决方案3】:
    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];    
        self.navigationController.navigationBar.alpha = 0;
    }
    - (void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
        self.navigationController.navigationBar.alpha = 1;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多