In iOS 13 modal presentations using the form and page sheet style can be dismissed with a pan down gesture. This is problematic in one of my form sheets because the user draws into this box which interferes with the gesture. It pulls the screen down instead of drawing a vertical line.
How can you disable the vertical swipe to dismiss gesture in a modal view controller presented as a sheet?
Setting isModalInPresentation = true still allows the sheet to be pulled down, it just won't dismiss.
In general, you shouldn't try to disable the swipe to dismiss functionality, as users expect all form/page sheets to behave the same across all apps. Instead, you may want to consider using a full-screen presentation style. If you do want to use a sheet that can't be dismissed via swipe, set isModalInPresentation = true, but note this still allows the sheet to be pulled down vertically and it'll bounce back up upon releasing the touch. Check out the UIAdaptivePresentationControllerDelegate documentation to react when the user tries to dismiss it via swipe, among other actions.
If you have a scenario where your app's gesture or touch handling is impacted by the swipe to dismiss feature, I did receive some advice from an Apple engineer on how to fix that.
If you can prevent the system's pan gesture recognizer from beginning, this will prevent the gestural dismissal. A few ways to do this:
If your canvas drawing is done with a gesture recognizer, such as your own UIGestureRecognizer subclass, enter the began phase before the sheet’s dismiss gesture does. If you recognize as quickly as UIPanGestureRecognizer, you will win, and the sheet’s dismiss gesture will be subverted.
If your canvas drawing is done with a gesture recognizer, setup a dynamic failure requirement with -shouldBeRequiredToFailByGestureRecognizer: (or the related delegate method), where you return NO if the passed in gesture recognizer is a UIPanGestureRecognizer.
If your canvas drawing is done with manual touch handling (e.g. touchesBegan:), override -gestureRecognizerShouldBegin on your touch handling view, and return NO if the passed in gesture recognizer is a UIPanGestureRecognizer.
With my setup #3 proved to work very well. This allows the user to swipe down anywhere outside of the drawing canvas to dismiss (like the nav bar), while allowing the user to draw without moving the sheet, just as one would expect.
I cannot recommend trying to find the gesture to disable it, as it seems to be rather dynamic and can reenable itself when switching between different size classes for example, and this could change in future releases.
This gesture can be found in the modal view controller's presentedView property. As I debugged, the gestureRecognizers array of this property has only one item and printing it resulted in something like this:
UIPanGestureRecognizer: 0x7fd3b8401aa0
(_UISheetInteractionBackgroundDismissRecognizer);
So to disable this gesture you can do like below:
let vc = UIViewController()
self.present(vc, animated: true, completion: {
vc.presentationController?.presentedView?.gestureRecognizers?[0].isEnabled = false
})
To re-enable it simply set isEnabled back to true:
vc.presentationController?.presentedView?.gestureRecognizers?[0].isEnabled = true
Note that iOS 13 is still in beta so a simpler approach might be added in an upcoming release.
Although this solution seems to work at the moment, I would not recommend it as it might not work in some situations or might be changed in future iOS releases and possibly affect your app.
Use this in the presented ViewController viewDidLoad:
if #available(iOS 13.0, *) {
self.isModalInPresentation = true
}
In my case, I have a modal screen with a view that receives touches to capture customer signatures.
Disabling the gesture recognizer in the navigation controller solved the problem, preventing the modal interactive dismissal from being triggered at all.
The following methods are implemented in our modal view controller, and are called via delegate from our custom signature view.
Called from touchesBegan:
private func disableDismissalRecognizers() {
navigationController?.presentationController?.presentedView?.gestureRecognizers?.forEach {
$0.isEnabled = false
}
}
Called from touchesEnded:
private func enableDismissalRecognizers() {
navigationController?.presentationController?.presentedView?.gestureRecognizers?.forEach {
$0.isEnabled = true
}
}
Here is a GIF showing the behavior:
This question, flagged as duplicate, describes better the issue I had: Disabling interactive dismissal of presented view controller on iOS 13 when dragging from the main view
you can change the presentation style, if its in full screen the pull down to dismiss would be disabled
navigationCont.modalPresentationStyle = .fullScreen
No need to reinvent the wheel. It is as simple as adopting the UIAdaptivePresentationControllerDelegate protocol on your destinationViewController and then implement the relevant method:
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
return false
}
For example, let's suppose that your destinationViewController is prepared for segue like below:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourIdentifier",
let destinationVC = segue.destination as? DetailViewController
{
//do other stuff
destinationVC.presentationController?.delegate = destinationVC
}
}
Then on the destinationVC (that should adopt the protocol described above), you can implement the described method func presentationControllerShouldDismiss(_ presentationController:) -> Bool or any of the other ones, in order to handle correctly your custom behaviour.
You can use the UIAdaptivePresentationControllerDelegate method presentationControllerDidAttemptToDismiss and disable the gestureRecognizer on the presentedView.
Something like this:
func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
presentationController.presentedView?.gestureRecognizers?.first?.isEnabled = false
}
For every body having problems with Jordans solution #3 running.
You have to look for the ROOT viewcontroller which is beeing presented, depending on your viewstack, this is maybe not you current view.
I had to look for my navigation controllers PresentationViewController.
BTW #Jordam: Thanks!
UIGestureRecognizer *gesture = [[self.navigationController.presentationController.presentedView gestureRecognizers] firstObject];
if ([gesture isKindOfClass:[UIPanGestureRecognizer class]]) {
UIPanGestureRecognizer * pan = (UIPanGestureRecognizer *)gesture;
pan.delegate = self;
}
You may first get a reference to the UIPanGestureRecognizer handling the page sheet dismissal in viewDidAppear() method. Notice that this reference is nil in viewWillAppear() or viewDidLoad(). Then you simply disable it.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
presentationController?.presentedView?.gestureRecognizers?.first.isEnabled = false
}
If you want more customization rather than disabling it completely, for example, when using a navBar within the page sheet, set the delegate of that UIPanGestureRecognizer to your own view controller. That way, you can disable the gesture recognizer exclusively in your contentView while keeping it active in your navBar region by implementing
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {}
in IOS 13
if #available(iOS 13.0, *) {
obj.isModalInPresentation = true
} else {
// Fallback on earlier versions
}
Me, I use this :
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
for(UIGestureRecognizer *gr in self.presentationController.presentedView.gestureRecognizers) {
if (#available(iOS 11.0, *)) {
if([gr.name isEqualToString:#"_UISheetInteractionBackgroundDismissRecognizer"]) {
gr.enabled = false;
}
}
}
Will try to describe method 2 already suggested by #Jordan H in more details:
1) To be able to catch and make decisions about the modal sheet's pan gesture add this into view controller's viewDidLoad:
navigationController?.presentationController?.presentedView?.gestureRecognizers?.forEach {
$0.delegate = self
}
2) Enable the ability to catch the pan gesture together with your own gestures using gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
3) The actual decision can go in gestureRecognizer(_:shouldBeRequiredToFailBy:)
Example code, which makes the swipe gesture to be preferred over sheet's pan gesture, if both present. It doesn't affect original pan gesture in areas where there is no swipe gesture recognizer and therefore the original "swipe to dismiss" can still work as designed.
extension PeopleViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer === UIPanGestureRecognizer.self && otherGestureRecognizer === UISwipeGestureRecognizer.self {
return true
}
return false
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
In my case I have only a few swipe gesture recognizers, so comparing types is enough for me, but if there more of them it might make sense to compare the gestureRecognizers themselves (either programmatically added ones or as outlets from interface builder) as described in this doc: https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/coordinating_multiple_gesture_recognizers/preferring_one_gesture_over_another
Here's how the code works in my case. Without it the swipe gesture was mostly ignored and worked only occasionally.
In the case when a UITableView or UICollectionView initiates the page sheet dismiss gesture when the user attempts to scroll past the top end of the scrolling view, this gesture can be disabled by adding an invisible UIRefreshControl that calls endRefreshing immediately.
See also https://stackoverflow.com/a/58676756/2419404
SwiftUI since iOS 15
.interactiveDismissDisabled()
For Example:
.sheet(isPresented: $add) {
AddView()
.interactiveDismissDisabled()
}
For navigation Controller, to avoid swipe interaction for presented view we can use:
if #available(iOS 13.0, *) {navController.isModalInPresentation = true}
In prepare(for:sender:) :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == viewControllerSegueID {
let controller = segue.destination as! YourViewController
controller.modalPresentationStyle = .fullScreen
}
}
or, after you initialize your controller:
let controller = YourViewController()
controller.modalPresentationStyle = .fullScreen
I've come across the following, which I believe is a bug in iOS 12. This worked fine in iOS 11.4.1. Try the following.
Open a new project in Xcode 10. Add a UI button. Add the following to your PLIST Privacy - Camera Usage Description with some description.
Copy the following code:
import UIKit
class ViewController: UIViewController {
let imagePickerController = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func takePic(_ sender: Any) {
takePicture()
}
func takePicture() {
imagePickerController.allowsEditing = false
imagePickerController.sourceType = UIImagePickerController.SourceType.camera
imagePickerController.cameraCaptureMode = .photo
imagePickerController.modalPresentationStyle = .fullScreen
present(imagePickerController, animated: true, completion: nil)
}
}
Wire up the UIButton to the takePic IBAction.
Run the app on an iOS 12 device, since the simulator doesn't have a camera. The UIImagePickerController should show the camera.
Now remove Portrait from the Device Orientation in Xcode Targets-> Deployment Info. Run again and the app will crash with the following:
*** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [CAMViewfinderViewController shouldAutorotate] is returning YES'
*** First throw call stack:
(0x1d1ecff78 0x1d10c8284 0x1d1dd075c 0x1ff912b30 0x1ff913130 0x1ff9139a0 0x1ff8fcff0 0x1ff8d70bc 0x1ff8d6e84 0x1ff8d6e84 0x1ff8d6e84 0x1ff8d6e84 0x1ff8c9968 0x1ff8c982c 0x1ff8d9d88 0x1ff894ab4 0x1ff91dbb4 0x1ff550888 0x1ff904430 0x1ff21171c 0x1ff1fed44 0x1ff22fa84 0x1d1e5bfe0 0x1d1e56ab8 0x1d1e5703c 0x1d1e56844 0x1d4105be8 0x1ff205428 0x102f9d9f4 0x1d190c020)
libc++abi.dylib: terminating with uncaught exception of type NSException
This used to work on devices on iOS 11...
Since I have an app that is now crashing, how do I somehow trick the app to tricking the app is in portrait mode before presenting the UIImagePickerController?
UPDATE: Appears to only crash on iPhone X/XS
Thanks.
I had the same problem with an iPhone X on iOS 12, what i did to solve it is :
Not setting any interface orientation in the project file
Defining an initial interface orientation in the info.plist file, using the key Initial interface orientation and the value Landscape (right home button)
Finally, in each of your UIViewController, you should define the interface orientation using for example:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscape
}
I'm curious as to what the presentViewController does with the first parameter, imagePickerController. Given the way I assigned imagePickerController's properties, will the presentViewController use all that information to display the view?
#IBAction func imageFromPhotoLibrary(sender: UITapGestureRecognizer) {
// Hide the keyboard.
myTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .PhotoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
presentViewController won't use any of the options. The imagePickerController you passed in will use those options.
Since the Apple APIs are pretty well designed, presentViewController does one thing (SRP), and does exactly what it says it does... presents the view controller you pass it.
The imagePickerController is in charge controlling the view of the image picker.
I am new to Swift/app development. I have successfully developed an app that uses an UIImagePickerController to let the user select an image from the device's photo library. However, I would like the user to select images from a custom set of images, and not from the device's photo library.
What would be the easiest way to implement this? Any help appreciated!
Edit
As per requested here is some of the code I am using.
I have a view controller that adheres to the following protocols:
class StockViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
On my main storyboard, I have an UIImageView with a tap gesturer which triggers an action on selectImageFromPhotoLibrary.
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
// Hide the keyboard.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .PhotoLibrary
imagePickerController.allowsEditing = false
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
The only source types I can select for the imagePickerController are SavedPhotoAlbums, Camera or PhotoLibrary. I cannot select any app image folder.
I am making one custom keyboard in which when orientation of mobile is changed then in landscape i have to hide one button please suggest how can i do this i am using below code to do this task please help me.
in landscape also the button is visible i want to hide button on landscape and visible in portrait mode
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
var currentDevice: UIDevice = UIDevice.currentDevice()
var orientation: UIDeviceOrientation = currentDevice.orientation
if orientation.isLandscape {
button.hidden = true
}
if orientation.isPortrait {
button.hidden = false
}
}
In your viewDidLoad put
NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationChanged", name: UIDeviceOrientationDidChangeNotification, object: nil)
Then add this method
func orientationChanged()
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
button.hidden = true
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
button.hidden = false
}
}
Note:
UIDevice.currentDevice().orientation might not give you the correct orientation. You can use the status bar orientation instead UIApplication.sharedApplication().statusBarOrientation
From the apple docs:
UIDevice.currentDevice().orientation
The value of the property is a constant that indicates the current
orientation of the device. This value represents the physical
orientation of the device and may be different from the current
orientation of your application’s user interface. See
“UIDeviceOrientation” for descriptions of the possible values.