xcode 如何隐藏导航栏而不失去滑回能力

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26332216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:51:20  来源:igfitidea点击:

How to hide Navigation Bar without losing slide-back ability

iosobjective-cxcode

提问by Zinan Xing

I have a UITableView and it has a nav bar(got from UINavigationViewController), it's able to go back by sliding back using a finger.

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

I tried to hide the nav bar but keep the slide-back ability, code:

我试图隐藏导航栏但保持滑回能力,代码:

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

This successfully hid the nav bar, however, I can no longer slide back to the last screen either.

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

Is there any way to hide the nav bar but keep the slide-back ability?

有什么办法可以隐藏导航栏但保持滑回能力吗?

采纳答案by Zinan Xing

Found the solution:

找到了解决办法:

- (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;
}

And in .h file, conform to UIGestureRecognizerDelegate

并且在 .h 文件中,符合 UIGestureRecognizerDelegate

回答by Ber.to

Tested with Swift 2 the solution of @gabbler, if you use

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

self.navigationController?.navigationBar.hidden = true

Swift 3.0

斯威夫特 3.0

self.navigationController?.navigationBar.isHidden = true

instead of

代替

self.navigationController?.navigationBarHidden = true

the swipe back gesture works like a charm!

向后滑动手势就像一个魅力!

回答by gabbler

Use

self.navigationController.navigationBar.hidden = YES;

or add this line in viewWillAppear:

或在 viewWillAppear 中添加这一行:

self.navigationController.interactivePopGestureRecognizer.delegate = self;

It seems the interaction is not effective, adding this line and make the view controller conforms to the UIGestureRecognizerDelegate protocol will make it work.

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

回答by Wyetro

Make sure to include:

确保包括:

self.navigationController.navigationBar.hidden = YES;

And:

和:

self.navigationController.interactivePopGestureRecognizer.delegate = self;

And:

和:

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

It should appear like this:

它应该是这样的:

- (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;
}

回答by atereshkov

Swift 4.x and iOS 11.4.

Swift 4.x 和 iOS 11.4。

The @gabbler solution is still working. Idk, looks like this is a UIKit bug, but..

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

Just use:

只需使用:

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
}

回答by Sean Stayns

Zinan Xings solution in Swift 4.2 (Please give him upvote!):

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

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
}

回答by Abdullah Tahan

for Xamarin Forms i was Struggle with this so first don't NavigationRenderer you'll get NavigationController null instead use PageRenderer:

对于 Xamarin 表单,我一直在为此苦苦挣扎,因此首先不要使用 NavigationRenderer,您将获得 NavigationController null 而不是使用 PageRenderer:

[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;

        }
    }
}

回答by Prabhu.Somasundaram

If hiding the the navigation bar did't help, try changing the rect of the navigation bar and see?

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

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

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