Disable swipe back gesture in Swift
You could disable it but that would not be to recommended as most iOS users go back by swiping and less by pressing the back button.
If you want to disable it it would be more reasonable to use a modal segue
instead of a push segue which is not that big of a transfer.
If you really want to get rid of the swipe to go back function I would just disable the back button and have a done button on the top right of the screen.
self.navigationController?.navigationItem.backBarButtonItem?.isEnabled = false;
How to disable back/left swipe gesture?
From the controller you want this to be enabled/disabled just
Swift:
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false // or true
ObjC:
self.navigationController.interactivePopGestureRecognizer.enabled = NO; // or YES
How to disable back swipe gesture in UINavigationController on iOS 7
I found a solution:
Objective-C:
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
Swift 3+:self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
Disable swipe-back for a NavigationLink SwiftUI
By hiding the back-button in the navigation bar, the swipe-back gesture is disabled. You can set a custom back-button with .navigationBarItems()
struct ContentView: View {
var body: some View {
NavigationView{
List{
NavigationLink(destination: Text("You can swipe back")){
Text("Child 1")
}
NavigationLink(destination: ChildView()){
Text("Child 2")
}
}
}
}
}
struct ChildView: View{
@Environment(\.presentationMode) var presentationMode
var body:some View{
Text("You cannot swipe back")
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: Button("Back"){self.presentationMode.wrappedValue.dismiss()})
}
}
How to disable swipe back gesture with master-detail view in ios 11?
I figured it out. I had to add this to my DetailViewController
id savedGestureRecognizerDelegate1;
id savedGestureRecognizerDelegate2;
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
savedGestureRecognizerDelegate1 = self.navigationController.interactivePopGestureRecognizer.delegate;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
if ([self.navigationController.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
savedGestureRecognizerDelegate2 = self.navigationController.navigationController.interactivePopGestureRecognizer.delegate;
self.navigationController.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = savedGestureRecognizerDelegate1;
}
if ([self.navigationController.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.navigationController.interactivePopGestureRecognizer.delegate = savedGestureRecognizerDelegate2;
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self.navigationController.interactivePopGestureRecognizer || gestureRecognizer == self.navigationController.navigationController.interactivePopGestureRecognizer) {
return NO;
}
return YES;
}
This is because there's two navigation controllers in a split view controller. The saved gesture recognizer delegate is because of this.
How to disable swipe back specifically on only one view controller
A better approach is to clear them from the stack when you show UserProfileVC
let profile = self.storyboard?.instantiateViewController(withIdentifier: "profileID") as! UserProfileVC
self.navigationController?.viewControllers = [profile]
Edit: Do this inside profileVC
self.navigationController?.viewControllers = [self]
//
self.view.alpha = 0
UIView.animate(withDuration: 0.5) {
self.view.alpha = 1
}
Overriding back swipe gesture in UINavigationController
You can do this in a combination of the following:
Add a swipe gesture recognizer to your view controller:
Add the following to your view controller class:
import UIKit
class SwipeBackViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
@IBAction func swipeback(_ sender: UISwipeGestureRecognizer) {
navigationController?.popToRootViewController(animated: true)
}
}
- The command in
viewDidLoad
disables the default swipe recogninzer in iOS - Then, the action associated with the swipe recognizer you added above handles the pop for you
My answer here goes into disabling the recognizer in more detail in case you have any questions on that.
Related Topics
How to Customize the Navigation Back Symbol and Navigation Back Text
How to Test My Xcode 7.2-Compiled App with iOS 9.3
Nsdateformatter: Date According to Currentlocale, Without Year
iPhone - Convert Ctfont to Uifont
Redirect Nslog to File in Swift Not Working
Unexpected Behavior When Casting an Nsnumber to Float
Sort Array of Dictionaries by Key Value
Why Can't I Invert My Image Back to Original with Cifilter in My Swift iOS App
Get Random Child from Firebase Database
iOS 7 Parallax Effect in My View Controller
Implementing Fast and Efficient Core Data Import on iOS 5
Xcode 10: a Valid Provisioning Profile for This Executable Was Not Found
Images Can't Contain Alpha Channels or Transparencies
Dashed Line Border Around Uiview
Custom Cell Row Height Setting in Storyboard Is Not Responding
Uiimageview Missing Images in Launch Screen on Device
Why Is Uiscrollview Leaving Space on Top and Does Not Scroll to the Bottom