Prevent swiping between web pages in iPad Safari
In iOS 13.4+ you can now preventDefault
on "touchstart"
Let's say we have a <div class="block-swipe-nav">
on the page that spans the entire width of the viewport and we want to prevent swipe navigation on that element.
const element = document.querySelector('.block-swipe-nav');
element.addEventListener('touchstart', (e) => {
// is not near edge of view, exit
if (e.pageX > 10 && e.pageX < window.innerWidth - 10) return;
// prevent swipe to navigate gesture
e.preventDefault();
});
I've written a short article on blocking swipe with some additional information.
Swipe back in navigation controller on iOS 7 with backbutton hidden
I found a nice way to add this great animation if you have a custom backbutton:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithImage:img
style:UIBarButtonItemStylePlain
target:self
action:@selector(onBack:)];
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
Swipe gesture for back/forward in UIWebView?
Why not just use a swipe gesture recognizer?
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on WebView
[webView addGestureRecognizer:swipeLeft];
[webView addGestureRecognizer:swipeRight];
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"Left Swipe");
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"Right Swipe");
}
}
Related Topics
How to Combine Two Dictionary Instances in Swift
How to Make a Function Execute Every Second in Swift
How to Dismiss Keyboard iOS Programmatically When Pressing Return
Add Uipickerview in Uiactionsheet from iOS 8 Not Working
How to Dismiss a Storyboard Popover
Tap on a Part of Text of Uilabel
Show iPhone Cut Copy Paste Menu on Uilabel
Swift: How to Refresh Uicollectionview Layout After Rotation of the Device
Cropping Image with Swift and Put It on Center Position
Getting Text from Image on iOS (Image Processing)
Default Tab Bar Item Colors Using Swift Xcode 6
Uisearchbar: Clear Background Color or Set Background Image
Combine Onchange and Onappear Events in Swiftui View
Astoryboard.Instantiateviewcontrollerwithidentifier("Myid") Returns Nil But Not Nil in Lldb