如何隐藏导航栏而不会失去滑回功能

时间:2014-10-13 02:44:24

标签: ios objective-c xcode

我有一个UITableView,它有一个导航栏(来自UINavigationViewController),它可以通过用手指向后滑动返回。

我试图隐藏导航栏但保留了滑回功能,代码:

- (void)viewWillAppear:(BOOL)animated {
    [[self navigationController] setNavigationBarHidden:YES animated:YES];
}

这成功隐藏了导航栏,但是,我无法再滑回到最后一个屏幕。

有没有办法隐藏导航栏但保持滑回功能?

9 个答案:

答案 0 :(得分:50)

使用Swift 2测试@gabbler的解决方案,如果您使用

self.navigationController?.navigationBar.hidden = true

Swift 3.0

self.navigationController?.navigationBar.isHidden = true

而不是

self.navigationController?.navigationBarHidden = true
轻扫手势就像一个魅力!

答案 1 :(得分:20)

找到解决方案:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // hide nav bar
    [[self navigationController] setNavigationBarHidden:YES animated:YES];

    // enable slide-back
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return YES;
}

在.h文件中,符合UIGestureRecognizerDelegate

答案 2 :(得分:7)

使用

self.navigationController.navigationBar.hidden = YES;

或在viewWillAppear中添加此行:

self.navigationController.interactivePopGestureRecognizer.delegate = self;

似乎交互无效,添加此行并使视图控制器符合UIGestureRecognizerDelegate协议将使其工作。

答案 3 :(得分:3)

确保包括:

self.navigationController.navigationBar.hidden = YES;

self.navigationController.interactivePopGestureRecognizer.delegate = self;

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }

应该是这样的:

- (void)viewWillAppear:(BOOL)animated {

    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return YES;
}

答案 4 :(得分:2)

Swift 4.x和iOS 11.4。

@gabbler解决方案仍然有效。 Idk,看起来这是一个UIKit错误,但是..

只需使用:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationController?.navigationBar.isHidden = true
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    self.navigationController?.navigationBar.isHidden = false
}

答案 5 :(得分:1)

对于Xamarin Forms我正在努力奋斗,所以首先不要使用NavigationRenderer,而是使用PageRenderer取代NavigationController:

[assembly: Xamarin.Forms.ExportRenderer(typeof(ContentPage), typeof(ContentPageRenderer))]
namespace sample
{
    class ContentPageRenderer : PageRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewDidAppear(animated);

            var navctrl = this.ViewController.NavigationController;
            navctrl.InteractivePopGestureRecognizer.Delegate = new UIGestureRecognizerDelegate();
            navctrl.InteractivePopGestureRecognizer.Enabled = true;

        }
    }
}

答案 6 :(得分:0)

如果隐藏导航栏没有帮助,请尝试更改导航栏的矩形并查看?

navBarBgFrame.origin.y = - navBarBgFrame.size.height;

答案 7 :(得分:0)

Swift 4.2中的Zinan Xings解决方案(请给他点赞!)

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationController?.setNavigationBarHidden(true, animated: true)
    self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    self.navigationController?.interactivePopGestureRecognizer?.delegate = self
}

答案 8 :(得分:0)

这是Swift 5的有效代码:

private func setupNavController() {
            
    navigationController?.setNavigationBarHidden(true, animated: true)     
    self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
    if let interactivePopGestureRecognizer = navigationController?.interactivePopGestureRecognizer {
        self.tableView.panGestureRecognizer.require(toFail: interactivePopGestureRecognizer)
    }
}

我有一个UIViewController,其中包含一个UITableView。此代码可帮助您在向后滑动或按返回按钮时保持平滑过渡。在viewWillAppear中调用此函数。