在iPad上使用UIAlertController的UIAlertControllerStyleActionSheet样式弹出相机还是相册选择框时,程序崩溃,信息如下
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x7fe167894000>) of style UIAlertControllerStyleActionSheet from MainTabBarViewController (<MainTabBarViewController: 0x7fe16709e000>). The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'
意思就是没有设置UIAlertController这个弹出窗口的位置信息,可以通过下面的方式解决
/// iPad上需要设置UIAlertController这个弹出窗口的位置信息(当使用UIAlertController的UIAlertControllerStyleActionSheet时在iPad上运行会崩溃)
if (isPad) {
alertVC.popoverPresentationController.sourceView = self.vMe.table;
alertVC.popoverPresentationController.sourceRect = self.view.frame;
}
或者是通过实现 UIPopoverPresentationControllerDelegate的prepareForPopoverPresentation方法 来设置UIAlertController在当前页面上的位置信息,设置后的显示效果与 iPhone是有区别的不是在屏幕的中间位置弹出而是在你所设置的位置弹出:如下图的界面效果
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
/// iPad上需要设置UIAlertController这个弹出窗口的位置信息(当使用UIAlertController的UIAlertControllerStyleActionSheet时在iPad上运行会崩溃)
if (isPad) {
alertVC.popoverPresentationController.sourceView = self.vMe.table;
alertVC.popoverPresentationController.sourceRect = self.view.frame;
}
/// 取消
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:BZLocalizedString(@"common_cancel", nil)
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
}];
/// 相册
UIAlertAction *photoAlbumAction = [UIAlertAction actionWithTitle:BZLocalizedString(@"common_photoAlbum", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[weakSelf showPickerFromPhotoAlbum];
}];
/// 拍照
UIAlertAction *takingPicturesAction = [UIAlertAction actionWithTitle:BZLocalizedString(@"common_takingPictures", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[weakSelf showPickerFromCameraSource];
}];
[alertVC addAction:cancelAction];
[alertVC addAction:takingPicturesAction];
[alertVC addAction:photoAlbumAction];
/// 展示
[weakSelf presentViewController:alertVC animated:YES completion:nil];