當使用UIAlertController的app
UIAlertControllerStyleActionSheetide
時在ipad上運行會崩潰,報如下的錯誤:this
reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x1099a7800>) of style UIAlertControllerStyleActionSheet. 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.spa
意思就是沒有設置UIAlertController這個彈出窗口的位置信息,能夠經過下面的方式解決,orm
alertSheetVc.popoverPresentationController.sourceView = self.bgScrollView;
alertSheetVc.popoverPresentationController.sourceRect = view.frame;
或者是經過實現 UIPopoverPresentationControllerDelegate的prepareForPopoverPresentation方法 來設置UIAlertController在當前頁面上的位置信息,設置後的顯示效果與 iPhone是有區別的不是在屏幕的中間位置彈出而是在你所設置的位置彈出:以下圖的界面效果blog
UIAlertController *alertSheetVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; alertSheetVc.popoverPresentationController.sourceView = self.bgScrollView; alertSheetVc.popoverPresentationController.sourceRect = view.frame; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"去相冊選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self pushTZImagePickerController]; }]; UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self takePhoto]; }]; [alertSheetVc addAction:cameraAction]; [alertSheetVc addAction:albumAction]; [alertSheetVc addAction:cancelAction]; [self presentViewController:alertSheetVc animated:YES completion:nil];