The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover.-iOS

因当前 App 程序需要同时兼容 iPhone 和 iPad 两种机型,在使用 UIAlertController 提示框的时候遇到了兼容性的问题,具体 Crash 信息如下所示:

The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. 
You must provide location information for this popover through the alert controller's popoverPresentationController

底部弹出的 UIAlertControllerStyleActionSheet 方式仅支持 iPhone 设备;
中间弹出的 UIAlertControllerStyleAlert 方式同时兼容 iPhone 和 iPad;

 

解决办法

为了保持现有 iPhone 机型的同时改造 iPad,则在提示框封装的方法中新增了机型判断的方法,不同的设备机型匹配不同的 UIAlertControllerStyle 样式即可;

首先,判断设备类型,此次通过公共类中宏的方式已提早实现,在需要使用的地方调用即可;

/** 判断是否为iPhone*/
#define kISiPhone   (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
/** 判断是否为iPad*/
#define kISiPad     (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

其次,在自定义提示框类对应的方法中进行设备类型的判断和提示框样式的赋值即可;

UIAlertControllerStyle style = UIAlertControllerStyleActionSheet; // 注:UIAlertControllerStyleActionSheet 仅支持 iPhone,iPad 需使用 UIAlertControllerStyleAlert
if (kISiPad) {
    style = UIAlertControllerStyleAlert;
}

最后,完整的 code 如下:

/**
 调用相机或相册(默认后置摄像头,前置摄像头需将 deviceType 初始值设置为 @"Front")

 @param deviceType  摄像头设备类型
 @param controller  当前 VC 控件
 */
- (void)openCameraOrPhotoLibraryWithCameraDeviceType:(NSString *)deviceType AndController:(UIViewController *)controller {
    UIAlertControllerStyle style = UIAlertControllerStyleActionSheet; // 注:UIAlertControllerStyleActionSheet 仅支持 iPhone,iPad 需使用 UIAlertControllerStyleAlert
    if (kISiPad) {
        style = UIAlertControllerStyleAlert;
    }
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:nil
                                                                      message:nil
                                                               preferredStyle:style];
    
    kWeakSelf(self);
    [alertCon addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        [weakself openCameraWithCameraDeviceType:deviceType AndController:controller];
        
    }]];
    [alertCon addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakself openPhotoLibraryWithController:controller];
        
    }]];
    
    [alertCon addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [controller presentViewController:alertCon animated:YES completion:^{
        
    }];
}

Github自定义 UIAlertController 类


以上便是此次分享的全部内容,希望能对大家有所帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值