iOS
 Computer >> コンピューター >  >> プログラミング >> iOS

iOSでデフォルトのアラートダイアログ(UIAlertController)の幅と高さを制御する方法

iOSアプリを開発していると、アラートダイアログ(Alert)の幅や高さを自分好みに調整したい場面に出くわすことがあります。やり方を知らないと意外と戸惑ってしまうポイントですが、実はNSLayoutConstraintを使えば簡単にコントロールできます。

この記事では、デフォルトのアラートボックスの幅と高さを変更する手順を、サンプルプロジェクトを作りながら解説していきます。

UIAlertControllerの詳細については、Apple公式ドキュメントも参考にしてください。

参考:UIAlertController – Apple Developer Documentation

作成するサンプルについて

今回は新しいプロジェクトを作成し、画面上に配置したボタンをタップすると、カスタムサイズのアラートが表示されるというシンプルな構成にします。具体的な流れを見ていきましょう。

Step 1:プロジェクトを作成する

Xcodeを起動し、「New Project」→「Single View Application」を選択します。プロジェクト名は「changeheightandwidth」としましょう。

Step 2:ボタンを配置してアクションを接続する

Main.storyboardにボタンを1つ配置し、名前を「tap」に設定します。続いてViewController.swiftに@IBActionを作成し、アウトレット名は「btnAtap」とします。

Step 3:ボタンのメソッドにコードを記述する

まず、UIAlertControllerのオブジェクトを作成します。

let alert = UIAlertController(title: "Your Title", message: "Your Message", preferredStyle: UIAlertController.Style.alert)

次に、高さと幅の制約(Constraint)を生成して追加します。ここが今回のポイントです。

// 高さの制約
let constraintHeight = NSLayoutConstraint(
    item: alert.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:
    NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
alert.view.addConstraint(constraintHeight)

// 幅の制約
let constraintWidth = NSLayoutConstraint(
    item: alert.view!, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:
    NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 300)
alert.view.addConstraint(constraintWidth)

constantの値を変更することで、好きなサイズに調整できます。上記の例では高さ100pt・幅300ptに設定しています。

最後に、アクションボタンを追加してアラートを表示します。

let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(cancel)
let OKAY = UIAlertAction(title: "Done", style: .default, handler: nil)
alert.addAction(OKAY)
self.present(alert, animated: true, completion: nil)

Step 4:ビルドして実行する

コードを実行すると、指定したサイズのアラートが表示されるはずです。

完全なコード

@IBAction func btnATap(_ sender: Any) {
    let alert = UIAlertController(title: "Your Title", message: "Your Message", preferredStyle: UIAlertController.Style.alert)

    // 高さの制約
    let constraintHeight = NSLayoutConstraint(
        item: alert.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:
        NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
    alert.view.addConstraint(constraintHeight)

    // 幅の制約
    let constraintWidth = NSLayoutConstraint(
        item: alert.view!, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:
        NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 300)
    alert.view.addConstraint(constraintWidth)

    let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    alert.addAction(cancel)
    let OKAY = UIAlertAction(title: "Done", style: .default, handler: nil)
    alert.addAction(OKAY)
    self.present(alert, animated: true, completion: nil)
}

まとめ

UIAlertControllerは通常システムがサイズを自動決定しますが、NSLayoutConstraintで明示的に制約を追加することで、幅や高さを柔軟にカスタマイズできます。デザイン要件で特定のサイズが必要になったときは、ぜひこの手法を試してみてください。ただし、AppleのHuman Interface Guidelinesでは標準的なアラートの見た目が推奨されているため、乱用せず用途に応じて使い分けるのがおすすめです。

  1. Mac(OS X)とiPhone(iOS)でデフォルトのカレンダーを設定する方法

    MacやiPhoneのカレンダーアプリで新しい予定を作成すると、そのイベントは端末で「デフォルト」として設定されているカレンダーに自動的に追加されます。仕事用・プライベート用など複数のカレンダーを使い分けている場合、意図しないカレンダーに予定が登録されてしまうこともあるでしょう。幸い、OS XとiOSではデフォルトのカレンダーを簡単に変更できます。あらかじめ既定のカレンダーを指定しておけば、以降に作成するすべての予定がそこへ自動的に追加されるようになります。OS Xでデフォルトのカレンダーを設定する方法Macでは、標準搭載の「カレンダー」アプリからデフォルトのカレンダーを設定できます。1. D

  2. 【iPhone】iOS 11のコントロールセンターをカスタマイズする方法と新機能ガイド

    AppleがiPhoneおよびiPad向けにリリースした最新OS「iOS 11」では、コントロールセンターが全面的に刷新され、モジュール化されました。新しいコントロールセンターには3D Touchジェスチャーや新しいアニメーションが多数盛り込まれ、ユーザーの好みに合わせて自由にカスタマイズできるようになっています。さらに、これまでは別途アプリをダウンロードしなければ使えなかった便利な機能が、標準コントロールとして組み込まれたのも大きなポイントです。この記事では、iOS 11のコントロールセンターを自分好みにカスタマイズする手順と、新しく追加されたコントロールの詳細について解説します。iOS 1