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

Swiftを使用してiOSアプリでカスタムダイアログボックスを作成するにはどうすればよいですか?


すばやくダイアログボックスを作成するには、UIKitの重要な部分であるUIAlertControllerを使用します。これは、iOSアプリケーションとサンプルプロジェクトを使用して行います。

まず、空のプロジェクトを作成し、次にデフォルトのViewController内で次の操作を実行します。

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

let alert = UIAlertController.init(title: title, message: description, preferredStyle: .alert)

アクションを作成します

let okAction = UIAlertAction.init(title: "Ok", style: .default) { _ in
   print("You tapped ok")
   //custom action here.
}

アラートにアクションを追加して表示します

alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
これを関数に変換します-

func createAlert(withTitle title:String,andDescription description: String) {
   let alert = UIAlertController.init(title: title, message: description, preferredStyle: .alert)
   let okAction = UIAlertAction.init(title: "Ok", style: .default) {
       _ in print("You tapped ok")
      //custom action here.
   }
   alert.addAction(okAction)
   self.present(alert, animated: true, completion: nil)
}

次に、viewWillLayoutSubviewsメソッドで関数を呼び出します。これをデバイスで実行すると、次のようになります。

override func viewWillLayoutSubviews() {
   self.createAlert(withTitle: "This is an alert", andDescription: "Enter your description here.")
}

これにより、次のような結果が得られます。

Swiftを使用してiOSアプリでカスタムダイアログボックスを作成するにはどうすればよいですか?


  1. Androidアプリでカスタムアラートダイアログを作成するにはどうすればよいですか?

    この例は、AndroidでTextViewの特定のテキストを太字にする方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="ht

  2. CSSを使用してカスタムカーソルを作成する方法

    .cur(Internet Explorerの場合)、. gif、.png(Chrome、Firefox、Safariの場合)などの拡張子を持つカスタムカーソルイメージを作成し、CSSカーソルプロパティを使用して要素に適用し、URLに設定することができます。さらに、auto、default、pointerなどの一般的なカーソル値。 解決策 Selector {    cursor: url("/*path to custom cursor file*/"), generic cursor; } 例 例を使用してカスタムカーソルを作成する方法を見てみまし