UINavigationContoller swipe back function not working if its view is added to UIViewContoller

和自甴很熟 提交于 2019-12-24 06:00:37

问题


I have a UIViewController(RootViewController) set as the root view controller of the UIWindow. Any view / view controller is added to this RootViewController subview including a UINavigationController.

I found that there is a new behaviour in iOS7 for UINavigationController that you can swipe back to the previous view controller. But it doesn't not work for my case.

If I set the UINavigationController as the root view controller of the UIWindow. It works.

So my questions is why can't it swipe back if it is added to a UIViewController??

Updated:
It turns out that if the navigation bar is hidden, the swipe function will be disabled....


回答1:


This probably has something to do with not properly setting up view controller containment. I bet -viewDidLoad is not being called on your navigation controller's view controllers when you add the navigation controller's view as a subview to UIWindow's rootViewController. There's a good bit of literature out there on how to properly set up a custom view controller containment such that the right methods fire on your view controllers:

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

Also, if Apple used the transition API for this interaction its possible that this has something to do with either the navigation controller's delegate not being hooked up properly (as it would when navigation controllers are set as root view controllers' on UIWindow), or the parent view controller stealing/hiding the gesture. I would first take a look at the navigation controller's pop gesture delegate and see if it not nil:

NSLog(@"%@", self.navigationController.interactivePopGestureRecognizer.delegate);

If it's not nil then I would check that a gesture recognizer on the navigation controller can fire normally:

// In the navigation controller's root view controller, try this:
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move)];
[panRecognizer setMaximumNumberOfTouches:1];
[self.view addGestureRecognizer:panRecognizer];


// Then test to see if its working:
- (void)move {
    NSLog(@"Hello world.");
}


来源:https://stackoverflow.com/questions/22853010/uinavigationcontoller-swipe-back-function-not-working-if-its-view-is-added-to-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!