メールアプリのメニューに似たiPhoneのポップアップメニューを作成する 質問する

メールアプリのメニューに似たiPhoneのポップアップメニューを作成する 質問する

メッセージに返信したいときにメール アプリに表示されるポップアップ メニューに似たものを作成したいと思います。複数のアプリケーションでこれを見たことがあるので、そのためのフレームワークに何かが組み込まれているのか、それともサンプル コードがどこかにあるのかどうかはわかりませんでした。

UIActionSheet の例

ベストアンサー1

Swift でアクションシートを作成する

コードはSwift 5でテスト済み

ここに画像の説明を入力してください

iOS 8 以降では、UIAlertControllercombined がUIAlertControllerStyle.ActionSheet使用されます。UIActionSheetは非推奨です。

上記の画像のアクションシートを生成するコードは次のとおりです。

class ViewController: UIViewController {
    
    @IBOutlet weak var showActionSheetButton: UIButton!
    
    @IBAction func showActionSheetButtonTapped(sender: UIButton) {
        
        // Create the action sheet
        let myActionSheet = UIAlertController(title: "Color", message: "What color would you like?", preferredStyle: UIAlertController.Style.actionSheet)
        
        // blue action button
        let blueAction = UIAlertAction(title: "Blue", style: UIAlertAction.Style.default) { (action) in
            print("Blue action button tapped")
        }
        
        // red action button
        let redAction = UIAlertAction(title: "Red", style: UIAlertAction.Style.default) { (action) in
            print("Red action button tapped")
        }
        
        // yellow action button
        let yellowAction = UIAlertAction(title: "Yellow", style: UIAlertAction.Style.default) { (action) in
            print("Yellow action button tapped")
        }
        
        // cancel action button
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel) { (action) in
            print("Cancel action button tapped")
        }
        
        // add action buttons to action sheet
        myActionSheet.addAction(blueAction)
        myActionSheet.addAction(redAction)
        myActionSheet.addAction(yellowAction)
        myActionSheet.addAction(cancelAction)
        
        // present the action sheet
        self.present(myActionSheet, animated: true, completion: nil)
    }
}

まだヘルプが必要ですか? このビデオチュートリアルをご覧ください。これが私が学んだ方法です。

  • Swift の UIActionSheet の例(名前とは裏腹に、実際にUIAlertControllerは ではなく新しいアクション シートを使用しますUIActionSheet。)

おすすめ記事