UIActionSheet和UIAlertController在iPhone和iPad上的那些事兒。


整理一下今天的所得。應該從哪里說起呢?

從今天用pad測試工程的時候發現工程中拍照上傳沒法用了。我用iphone可以用呀。一開始以為是系統的問題,升級了ios10之后不是有對相冊,照相機等東西的私有設置嗎,設置了白名單之后還是不可以。然后我就用斷點打,發現是

    UIActionSheet *myActionSheet = [[UIActionSheet alloc]initWithTitle:nil

                                                              delegate:self

                                                     cancelButtonTitle:@"取消"

                                                destructiveButtonTitle:nil

                                                     otherButtonTitles:@"打開照相機",@"從手機相冊獲取",nil];

    

   [myActionSheet showInView:[UIApplication sharedApplication].keyWindow];

這段代碼的問題

查了一下UIActionSheet再iPhone 和iPad上的顯示是不同的。所以就改成了

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"獲取圖片" message:nil preferredStyle: UIAlertControllerStyleActionSheet];

    

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    

    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"打開照相機" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        [self takePhoto];

    }];

    

    UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"從手機相冊獲取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        [self LocalPhoto];

    }];

    

    [alertController addAction:cancelAction];

    

    [alertController addAction:deleteAction];

    

    [alertController addAction:archiveAction];

    alertController.popoverPresentationController.sourceView = [UIApplication sharedApplication].keyWindow.rootViewController.view;

    alertController.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0);

    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];

   

這些才終於解決了。我們用的庫是ZLPhotoLib,這是一個14年beiqing寫的庫。

注意哈:alertController.popoverPresentationController.sourceView = [UIApplication sharedApplication].keyWindow.rootViewController.view;

    alertController.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0);這兩句要是不指定可是會崩潰的這是UIAlertController的坑。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM