77
74

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UIAlertControllerをiPadで使用する際の注意点

Last updated at Posted at 2014-09-08

ポイント

  • スタイルは2種類UIAlertControllerStyleActionSheetUIAlertControllerStyleAlert
  • デフォルトはUIAlertControllerStyleActionSheet
  • スタイルは alertControllerWithTitle:message:preferredStyle以外では指定できない

対策

  • UIAlertControllerのpopoverPresentationControllerプロパティを使用する

iPadでUIAlertControllerStyleActionSheetpopoverPresentationControllerの指定なしに使うとクラッシュします

(@kishikawakatsumi さんご指摘ありがとうございます)

    UIAlertController *alertController
    = [UIAlertController alertControllerWithTitle:@"タイトル"
                                          message:@"メッセージ"
                                   preferredStyle:UIAlertControllerStyleActionSheet];

    [alertController addAction:[UIAlertAction actionWithTitle:@"アクション1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // アクション1 の処理
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"アクション2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // アクション2 の処理
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"閉じる" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}]];

    // iPad用の設定
    alertController.popoverPresentationController.sourceView = self.view;
    alertController.popoverPresentationController.sourceRect = CGRectMake(100.0, 100.0, 20.0, 20.0);
    //RectではなくUIBarButtonItemを起点にできるプロパティもある
    //alertController.popoverPresentationController.barButtonItem
    
    // 表示
    [self presentViewController:alertController animated:YES completion:^{}];

ac2.png

右iPad: 指定したRectを起点として"閉じる"(UIAlertActionStyleCancelで指定した部分)を除いて表示されます。外側をタップすることでアクションを実行せずに閉じることができます。

77
74
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
77
74

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?