メッセージに返信したいときにメール アプリに表示されるポップアップ メニューに似たものを作成したいと思います。複数のアプリケーションでこれを見たことがあるので、そのためのフレームワークに何かが組み込まれているのか、それともサンプル コードがどこかにあるのかどうかはわかりませんでした。
ベストアンサー1
Swift でアクションシートを作成する
コードはSwift 5でテスト済み
iOS 8 以降では、UIAlertController
combined が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
。)