Phi*_*son 62 uinavigationcontroller ios swift
在这里看了一会儿,但似乎无法找到一个有效的解决方案.
我试图在Swift中禁用滑动以返回上一个视图手势.
我尝试了各种解决方案,包括:
self.navigationController?.interactivePopGestureRecognizer.enabled = false
Run Code Online (Sandbox Code Playgroud)
和
self.navigationController.interactivePopGestureRecognizer.delegate = self
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer!) -> Bool {
return false
}
Run Code Online (Sandbox Code Playgroud)
是否有一种新方法可以做到这一点或其他方法有效?
Cod*_*der 128
以下是禁用和重新启用滑动的简单方法.
Swift 3.x及以上
在viewDidLoad/willAppear/didAppear方法中添加:
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
Run Code Online (Sandbox Code Playgroud)
请记住,如果你这样做viewDidLoad,那么下次打开视图时,它可能不会被设置,具体取决于它是否仍然在你的堆栈中.
除非你想让它保持关闭状态,则需要重新打开它时,认为通过任何封闭willMove(toParentViewController:)或willDisappear.你的navigationController意志是零viewDidDisappear,所以为时已晚.
navigationController?.interactivePopGestureRecognizer?.isEnabled = true
Run Code Online (Sandbox Code Playgroud)
关于SplitViewControllers的特别说明:
正如CompC在评论中指出的那样,您需要调用第二个导航控制器将其应用于详细视图:
navigationController?.navigationController?.interactivePopGe??stureRecognizer?.isE??nabled = false
Run Code Online (Sandbox Code Playgroud)
Swift 2.2和Objective-C
Swift版本2.x及以下版本:
navigationController?.interactivePopGestureRecognizer?.enabled
Run Code Online (Sandbox Code Playgroud)
Objective-C的:
self.navigationController.interactivePopGestureRecognizer.enabled
Run Code Online (Sandbox Code Playgroud)
Har*_*war 18
我能够通过在gestureRecognizerShouldBegin中返回false来做到这一点
class ViewController2: UIViewController, UIGestureRecognizerDelegate {
...
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationController?.interactivePopGestureRecognizer.delegate = self
}
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
return false
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*rck 13
您可以禁用它,但不建议这样做,因为大多数iOS用户通过滑动返回,而按下后退按钮则更少.如果你想要禁用它,那么使用一个modal segue不是那么大的转移的推送segue 会更合理.如果你真的想摆脱滑动返回功能,我会禁用后退按钮,并在屏幕的右上角有一个完成按钮.
self.navigationController?.navigationItem.backBarButtonItem?.isEnabled = false;
Run Code Online (Sandbox Code Playgroud)
小智 11
在将视图控制器推送到导航控制器之前添加此行
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
Run Code Online (Sandbox Code Playgroud)
Hari或Stefan的回答都没有错,但这更简洁.把它放在viewDidLoad中就可以了.
if navigationController!.respondsToSelector(Selector("interactivePopGestureRecognizer")) {
navigationController!.view.removeGestureRecognizer(navigationController!.interactivePopGestureRecognizer)
}
Run Code Online (Sandbox Code Playgroud)
编辑:
一个小警告是,如果导航控制器被另一个视图打开并且导航控制器关闭,那么您将收到EXC_BAD_ACCESS错误.要修复它,您必须保存原始的UIGestureRecognizer并在退出视图时将其放回原处.
宣布:
private var popGesture: UIGestureRecognizer?
Run Code Online (Sandbox Code Playgroud)
在删除手势之前立即:
popGesture = navigationController!.interactivePopGestureRecognizer
Run Code Online (Sandbox Code Playgroud)
然后在关闭视图时:
If popGesture != nil {
navigationController!.view.addGestureRecognizer(popGesture!)
}
Run Code Online (Sandbox Code Playgroud)
代替
self.navigationController.pushViewController(VC, animated: Bool)
称呼
self.navigationController.setViewContollers([VC], animated: Bool)
setViewControllers 替换堆栈上的所有 VC,而不是在顶部添加新控制器。这意味着新设置的VC是根VC,并且用户不能返回。
当您只想禁用单个 VC 上的滑动,并为其他 VC 保留向后滑动时,此方法最有效。
如果您希望用户能够返回,而不是通过滑动,请不要使用此方法,因为它会禁用所有返回(因为没有 VC 可以返回)。
| 归档时间: |
|
| 查看次数: |
47070 次 |
| 最近记录: |