Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating iPhone Pop-up Menu Similar to Mail App Menu

I'd like to create a pop-up menu similar to the one found in the mail app when you want to reply to a message. I've seen this in more than one application so I wasn't sure if there was something built into the framework for it or some example code out there.

UIActionSheet example

like image 882
LeeMobile Avatar asked May 26 '09 14:05

LeeMobile


2 Answers

Creating an Action Sheet in Swift

Code has been tested with Swift 5

enter image description here

Since iOS 8, UIAlertController combined with UIAlertControllerStyle.ActionSheet is used. UIActionSheet is deprecated.

Here is the code to produce the Action Sheet in the above image:

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)     } } 

Still need help? Watch this video tutorial. That's how I learned it.

  • UIActionSheet example in Swift (Contrary to the name, it actually does use the new UIAlertController action sheet rather than UIActionSheet.)
like image 97
Suragch Avatar answered Sep 30 '22 11:09

Suragch


It is a UIAlertController on iOS 8+, and a UIActionSheet on earlier versions.

like image 45
Rémy Avatar answered Sep 30 '22 11:09

Rémy