美文网首页
UIAlertControllerStyleActionShee

UIAlertControllerStyleActionShee

作者: CoderCurtis | 来源:发表于2017-12-08 14:39 被阅读132次

    ActionSheet样式弹框:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"act1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"act2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"cancle" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:action];
        [alert addAction:action2];
        [alert addAction:cancle];
        [self presentViewController:alert animated:YES completion:nil];
    

    设置仅在iPhone: 路径: TARGETS -> Development Info -> Devices

    Snip20171208_3.png

    上面创建UIAlertControllerStyleActionSheet样式弹框的代码在iPhone手机上运行时没有问题,运行效果如图(四周存在黑边是因为Xcode设置仅在iPhone运行 而模拟器放在了iPad上不适配所致): 此时可看到UIAlertControllerStyleActionSheet运行效果iPhone与iPad效果一致.

    Simulator Screen Shot - iPad Pro (9.7 inch) - 2017-12-08 at 14.21.09.png

    设置Universal:

    Snip20171208_2.png

    设置机型为Universal(即: 手机和iPad均支持) 在iPhone上运行效果与上一致,而在iPad上运行时 导致崩溃

    报错信息:

    Your application has presented a UIAlertController (<UIAlertController: 0x100b790f0>) 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.
    

    解决:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"act1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"act2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"cancle" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:action];
        [alert addAction:action2];
        [alert addAction:cancle];
        
        UIPopoverPresentationController *popover = alert.popoverPresentationController;
        if (popover)
        {
            popover.sourceView = self.view;
            popover.sourceRect = CGRectMake(0, 0, 100, 100);
            popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
        }
        
        [self presentViewController:alert animated:YES completion:nil];
    
    Simulator Screen Shot - iPad Pro (9.7 inch) - 2017-12-08 at 14.21.34.png

    相关文章

      网友评论

          本文标题:UIAlertControllerStyleActionShee

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