美文网首页iOS
The modalPresentationStyle of a

The modalPresentationStyle of a

作者: survivorsfyh | 来源:发表于2020-10-14 18:01 被阅读0次

    因当前 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 类


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

    相关文章

      网友评论

        本文标题:The modalPresentationStyle of a

        本文链接:https://www.haomeiwen.com/subject/hyvwpktx.html