【iOS13】UIImagePickerControllerでisModalInPresentationがtrueでもSwipe Down Closeできることに対する対処法
TL;DR
-
UIImagePickerController
をChild ViewControllerとしてもったViewControllerを定義
- イニシャライザの中で
UIViewController.isModalInPresentation
をtrue
にする
iOS13から入ったisModalInPresentation
でSwipe Down Closeを防げる
UIImagePickerController
をChild ViewControllerとしてもったViewControllerを定義
- イニシャライザの中で
UIViewController.isModalInPresentation
をtrue
にする
isModalInPresentation
でSwipe Down Closeを防げる
UIViewController
でmodalPresentationStyle
が.pageSheet
にセットされると、そのビューをSwipe Downで閉じることが可能です。1。
閉じられることの防止策の一つの手として、iOS13から入ったUIViewController.isModalInPresentation
をtrue
にするという手があります。
let vc = UIViewController()
// iOS13だとpresent時に、デフォルトで`vc.modalPresentationStyle` が `.pageSheet` のときと同様の動作になる
// Swipe Down Closeを防ぐために、isModalInPresentationをtrueにする
vc.isModalInPresentation = true
present(vc, animated: true, completion: nil)
UIImagePickerController
だとisModalInPresentation
をtrue
にしてもSwipe Down Closeを防げない
表題にもある通り、UIImagePickerController
はisModalInPresentation
をtrue
にしてもSwipe Down Closeを防げません。
let vc = UIImagePickerController()
// isModalInPresentationをtrueにしてもSwipe Down Closeできてしまいます
vc.isModalInPresentation = true
present(vc, animated: true, completion: nil)
対処方法
まず、UIImagePickerController
をChild ViewControllerとしてもったViewControllerを定義します。
このとき、イニシャライザの中でUIViewController.isModalInPresentation
をtrue
にしましょう。
@available(iOS 13.0, *) // `isModalInPresentation` がiOS13からなので
class ImagePickerControllerContainer: UIViewController {
// あえてイニシャライザでimagePickerを受け取るようにしたほうがDelegateの設定とかやりやすい
init(imagePicker: UIImagePickerController) {
super.init(nibName: nil, bundle: nil)
isModalInPresentation = true
addChild(imagePicker)
imagePicker.didMove(toParent: self)
view.addSubview(imagePicker.view)
}
...
このクラスはSwipe Down CloseできないImage Pickerとして利用することができます。
let picker = UIImagePickerController()
let vc = ImagePickerControllerContainer(imagePicker: picker)
present(vc, animated: true, completion: nil)
諸注意
かなりアドホックなクラスなので、プライベートクラスで定義して必用なときだけ使うほうが無難です。
// どっかのクラスのNested Typeとして定義したり
private class ImagePickerControllerContainer: UIViewController {
...
参考文献
- iOS13でのモーダル表示 - https://qiita.com/nullpo33/items/a46a180a0aaf63aadcea
-
iOS13だとdefaultで
.pageSheet
と同様の状態になります。というのも、iOS13だとmodalPresentationStyle
は、.automatic
がDefault Valueであり、直接.pageSheet
が指定されているわけではないです。https://developer.apple.com/documentation/uikit/uiviewcontroller/1621355-modalpresentationstyle ↩
Author And Source
この問題について(【iOS13】UIImagePickerControllerでisModalInPresentationがtrueでもSwipe Down Closeできることに対する対処法), 我々は、より多くの情報をここで見つけました https://qiita.com/freddi_/items/d734708878f8da5ecf4a著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .