簡體   English   中英

在 Swift 中禁用向后滑動手勢

[英]Disable swipe back gesture in Swift

在這里四處張望了一段時間,但似乎找不到可行的解決方案。

我試圖在 Swift 中禁用滑動到 go 回到上一個視圖手勢。

我嘗試了多種解決方案,包括:

self.navigationController?.interactivePopGestureRecognizer.enabled = false

self.navigationController.interactivePopGestureRecognizer.delegate = self

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer!) -> Bool {
    return false
}

有沒有新的方法可以做到這一點或其他一些有效的方法?

以下是禁用和重新啟用向后滑動的簡單方法。

Swift 3.x 及更高版本

在 viewDidLoad/willAppear/didAppear 方法中添加:

navigationController?.interactivePopGestureRecognizer?.isEnabled = false

請記住,如果您使用viewDidLoad執行此操作,那么下次您打開視圖時,可能不會根據它是否保留在您的堆棧中進行設置。

除非您希望它保持關閉狀態,否則您需要在通過willMove(toParentViewController:)willDisappear關閉視圖時將其重新打開。 您的navigationControllerviewDidDisappear將為零,所以為時已晚。

navigationController?.interactivePopGestureRecognizer?.isEnabled = true

關於SplitViewControllers的特別說明:

正如 CompC 在評論中指出的那樣,您需要調用第二個導航控制器以將其應用於詳細視圖,如下所示:

navigationController?.navigationController?.interactivePopGe‌​stureRecognizer?.isE‌​nabled = false

Swift 2.2 和 Objective-C

Swift 2.x 及以下版本:

navigationController?.interactivePopGestureRecognizer?.enabled

目標-C:

self.navigationController.interactivePopGestureRecognizer.enabled

您可以禁用它,但不建議這樣做,因為大多數 iOS 用戶通過滑動返回,按下返回按鈕則更少。 如果你想禁用它,使用modal segue而不是推送轉場會更合理,因為推送轉場不是那么大。 如果你真的想擺脫滑動返回功能,我只會禁用后退按鈕並在屏幕右上角有一個完成按鈕。

self.navigationController?.navigationItem.backBarButtonItem?.isEnabled = false;

我能夠通過在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
}

在將視圖控制器推送到導航控制器之前添加此行

self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false

Hari 或 Stefan 的回答都沒有錯,但這更簡潔。 只需將其放入 viewDidLoad 即可完成。

if navigationController!.respondsToSelector(Selector("interactivePopGestureRecognizer")) {
    navigationController!.view.removeGestureRecognizer(navigationController!.interactivePopGestureRecognizer)
}

編輯:

一個小小的警告是,如果 Navigation Controller 被另一個視圖打開並且 Navigation Controller 關閉,那么您將收到 EXC_BAD_ACCESS 錯誤。 要修復它,您必須保存原始 UIGestureRecognizer 並在退出視圖時將其放回原處。

宣布:

private var popGesture: UIGestureRecognizer?

在移除手勢之前:

popGesture = navigationController!.interactivePopGestureRecognizer

然后在關閉視圖時:

If popGesture != nil {
    navigationController!.view.addGestureRecognizer(popGesture!)
}

對於目標 -c

-(void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:true];

  self.navigationController.interactivePopGestureRecognizer.enabled = NO;

}

我通常確保在盡可能多的地方啟用向后滑動,甚至添加自定義手勢識別器以將其添加到模式屏幕。 但是,對於我的應用程序中的身份驗證和下載過程,我使用模態導航控制器開始該過程,然后為每個下一步推送視圖。 但是,一旦完成,我想阻止它們備份到身份驗證屏幕。

對於這種情況,我一直在使用:

navigationController?.interactivePopGestureRecognizer?.isEnabled = false
navigationItem.hidesBackButton = true

在最終屏幕上的viewWillAppear() 如果您正在推送另一個視圖並在那里需要它們,您可以在viewWillDisappear()撤消這些。

RowanPD 的Swift 4邏輯

private var popGesture: UIGestureRecognizer?

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

    if navigationController!.responds(to: #selector(getter: UINavigationController.interactivePopGestureRecognizer)) {
        self.popGesture = navigationController!.interactivePopGestureRecognizer
        self.navigationController!.view.removeGestureRecognizer(navigationController!.interactivePopGestureRecognizer!)
    }

}

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

    if let gesture = self.popGesture {
        self.navigationController!.view.addGestureRecognizer(gesture)
    }

}

代替

self.navigationController.pushViewController(VC, animated: Bool)

稱呼

self.navigationController.setViewContollers([VC], animated: Bool)

setViewControllers替換堆棧上的所有 VC,而不是在頂部添加新控制器。 這意味着新設置的 VC 是根 VC,用戶無法返回。

當您只想在單個 VC 上禁用滑動並為另一個 VC 保持滑動到背面時,這是最有效的。

如果您希望用戶能夠返回,而不是通過滑動,請不要使用此方法,因為它會禁用所有返回(因為沒有可返回的 VC)。

如果在您嘗試了所有方法后它不起作用,那么您就錯過了這一點。

  1. navigationController?.interactivePopGestureRecognizer?.isEnabled = false到您的 viewWillAppear(animated:) 方法。
  2. 如果它不起作用,請從視圖控制器中刪除導航委托。 再次檢查您的視圖控制器是否正在確認UINavigationControllerDelegateUIGestureRecognizerDelegate協議。 如果是這樣,只需將其刪除。

來這里有點晚了。 在我的例子self.navigationController?.navigationItem.backBarButtonItem?.isEnabled = false; 不工作。 所以我這樣做:您可以呈現視圖 controller 而不是推送視圖 controller。這樣,向后滑動手勢將不適用於視圖 controller。

navigationController?.present(vc, animated: true)

您可以為自定義后退按鈕使用 dismiss

self.dismiss(animated: true)

注意:您可以在呈現它之前設置 VC 模態呈現樣式以確保它是全屏的。

vc.modalPresentationStyle = .fullScreen

希望這有幫助。

如果需要在某些屏幕上顯示側邊菜單,則在此特定視圖上添加 AddScreenEdgePanGesture 而不是 navigationController 視圖

代替它

SideMenuManager.default.menuAddScreenEdgePanGesturesToPresent(toView: self.navigationController?.view)

有了這個

SideMenuManager.default.menuAddScreenEdgePanGesturesToPresent(toView: self.view)

如果您不關心系統后退按鈕的外觀(例如,如果您使用自定義后退按鈕或導航欄完全隱藏),它可能會幫助您:

navigationItem.hidesBackButton = true

它隱藏后退按鈕並禁用向后滑動手勢。

只有完全刪除手勢識別器才對我有用(從呈現視圖控制器)。

if let navigationController = parent.navigationController,
   let interactivePopGestureRecognizer = navigationController.interactivePopGestureRecognizer {
    navigationController.view.removeGestureRecognizer(interactivePopGestureRecognizer)
}

如果你不想回來,或者你設置了新的 rootViewController,請不要使用它。

self.navigationController.pushViewController(VC, animated: Bool)

用這個

self.navigationController.setViewContollers([VC], animated: Bool)

setViewControllers 刪除堆棧上的所有視圖控制器,然后用戶無法返回 go。 它將禁用所有后背

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM