请求照片访问时如何防止视图消失?

How to prevent view from disappearing when requesting photo access?

UIViewController 试图展示一个 UIImagePickerController,但该应用尚未请求访问照片库的权限。

因此在呈现 UIImagePickerController iOS 之前显示 UIAlertController 以询问用户允许或拒绝访问照片。但是,先前可见的视图已被删除,因此警报显示在底层视图控制器的视图上。

如何防止当前视图开始被删除或至少防止底层视图在显示警报时可见?

发现:

UIImagePickerController 必须将其 属性 modalPresentationStyle 设置为 .Over... 值,以便底层屏幕在转换完成后不会消失。

所以

self.modalPresentationStyle = .FullScreen

应该设置为其中之一

self.modalPresentationStyle = .OverFullScreen
self.modalPresentationStyle = .OverCurrentContext  // In this case also set `definesPresentationContext = true` for the presenting view controller.

来自documentation

OverFullScreen: A view presentation style in which the presented view covers the screen. The views beneath the presented content are not removed from the view hierarchy when the presentation finishes. So if the presented view controller does not fill the screen with opaque content, the underlying content shows through.