简体   繁体   English

iOS-通过在左侧边缘平移弹出视图控制器,导航栏消失

[英]iOS - pop a view controller by panning on the left edge, navigation bar disappears

So iOS 7 introduced this new feature that you can pop a view controller by panning on the left edge. 因此,iOS 7引入了此新功能,您可以通过在左侧边缘平移来弹出视图控制器。 Here is my problem: I have two view controllers, A and B, that are connected by a push segue. 这是我的问题:我有两个视图控制器A和B,它们通过推式按钮连接。 Both of the controllers have navigation bars (by embedding A in a navigation controller). 两个控制器都有导航栏(通过将A嵌入导航控制器中)。 The navigation bar in B will be hidden once the user enters B's scene, and can be shown if the user taps on the scene. 一旦用户进入B的场景,B中的导航栏将被隐藏,如果用户点击该场景,则可以显示。 If the user pans on the left edge of B while the navigation bar is hidden, the navigation bar in A will be hidden as well, which means that there is no way for the user to return further back from A. So is there a way to enforce A to always show the navigation bar regardless of B has hidden the bar or not? 如果在隐藏导航栏的同时用户在B的左边缘平移,则A中的导航栏也将被隐藏,这意味着用户无法从A进一步返回。强制A始终显示导航栏,而不管B是否隐藏了导航栏? Or is there a easy way to prevent the pan gesture from taking effect? 还是有一种简单的方法可以防止平移手势生效? I read this post which suggested a way of preventing the pan, but I can't locate the property in storyboard. 我阅读了这篇文章该文章提出了一种防止平移的方法,但我无法在情节提要中找到该属性。

EDIT: So I disabled the interactive pop gesture recognizer but that only solved half of the problem. 编辑:所以我禁用了交互式弹出手势识别器,但只能解决一半的问题。 The other half is that if I click the back button on the child view controller navigation bar when the navigation bar is disappearing, I am navigated back to the parent view controller without a navigation bar. 另一半是,如果在导航栏消失时单击子视图控制器导航栏上的后退按钮,那么我将导航回到没有导航栏的父视图控制器。 I tried calling [self.navigationController setNavigationBarHidden:NO] in viewWillAppear and then viewDidLoad but it does not work. 我尝试在viewWillAppear调用[self.navigationController setNavigationBarHidden:NO] ,然后调用viewDidLoad但是它不起作用。 Is this some sort of bug in the SDK or am I missing something? 是SDK中的某种错误,还是我缺少某些东西?

Here is the code for hiding the navigation bar in the child view controller 这是在子视图控制器中隐藏导航栏的代码

- (void)hideNavigationBar
{
    if (self.navigationBarHidden == NO)
    {
        [UIView animateWithDuration:UINavigationControllerHideShowBarDuration animations:^{
            self.navigationController.navigationBar.alpha = 0.0;
            self.previewCollectionView.alpha = 0.0;
        } completion:^(BOOL finished) {
            self.navigationBarHidden = YES;
        }];
    }
}

Yes, you can enforce the navigation bar's appearance in the A viewController's -viewWillAppear method. 是的,您可以在A viewController的-viewWillAppear方法中强制导航栏的外观。

Also, since you cannot find the interactivePopGestureRecognizer property in the storyboard, you can use this line in the A viewController's -viewDidLoad method: 另外,由于在情节提要中找不到interactivePopGestureRecognizer属性,因此可以在viewController的-viewDidLoad方法中使用以下行:

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

EDIT: 编辑:

In the viewWillAppear method , you will have to call: viewWillAppear method ,您将必须调用:

[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.alpha = 1.0;

I see a couple problems with your situation: 我发现您的情况有几个问题:

  1. You disable the interactive pop gesture and you hide the nav bar from view controller B. How is the user supposed to intuitively go back? 您禁用了交互式弹出手势,并从视图控制器B隐藏了导航栏。用户应该如何直观地返回?

  2. The animation that hides your navbar in B may be causing the issue. 将导航栏隐藏在B中的动画可能是造成此问题的原因。 If it's anything longer than a split second, that animation may not complete in time before you hit the back button and -viewWillAppear fires on A. 如果时间长于一瞬间,则该动画可能无法及时完成,请您按下“后退”按钮,并且-viewWillAppear会在A上触发。

  3. Your code in B hides the navigation bar for the navigation controller. 您在B中的代码隐藏了导航控制器的导航栏。 The navigation controller that holds view controller A is the same instance that holds view controller B. If you hide the navigation bar when B loads, then you go back to A (not sure how you're doing that without a back button or a edge pan gesture), it should still be hidden. 拥有视图控制器A的导航控制器与拥有视图控制器B的实例相同。如果在加载B时隐藏了导航栏,那么您将返回到A(不确定在没有后退按钮或边缘的情况下该怎么做)平移手势),则仍应将其隐藏。

You probably want NOT disable the gesture (so the user can intuitively go back) and turn the navigation bar back on in view controller A's -viewWillAppear , to cover the case where you turned it off in B: 您可能不希望禁用该手势(以便用户可以直观地返回),并在视图控制器A的-viewWillAppear重新打开导航栏,以解决您在B中将其关闭的情况:

if (self.navigationBarHidden == NO)
{
    self.navigationController.navigationBar.alpha = 1.0;
    self.previewCollectionView.alpha = 1.0;
    self.navigationBarHidden = NO;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM