Creating iPhone Pop-up Menu Similar to Mail App Menu

26,857

Solution 1

Check out the UICatalog example on Apple's website. The "Alerts" section has examples of how to use UIActionSheet to accomplish what you're trying to do.

Solution 2

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.

Solution 3

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

Solution 4

You need to use a UIActionSheet.

First you need to add UIActionSheetDelegate to your ViewController .h file.

Then you can reference an actionsheet with:

  UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Select Sharing option:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:
                        @"Share on Facebook",
                        @"Share on Twitter",
                        @"Share via E-mail",
                        @"Save to Camera Roll",
                        @"Rate this App",
                        nil];
   popup.tag = 1;
  [popup showInView:self.view];

Then you have to handle each of the calls.

- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {

  switch (popup.tag) {
    case 1: {
        switch (buttonIndex) {
            case 0:
                [self FBShare];
                break;
            case 1:
                [self TwitterShare];
                break;
            case 2:
                [self emailContent];
                break;
            case 3:
                [self saveContent];
                break;
            case 4:
                [self rateAppYes];
                break;
            default:
                break;
        }
        break;
    }
    default:
        break;
 }
}

This has been deprecated as of iOS 8.x.

https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html

Solution 5

This is how you'd do it in Objective-C on iOS 8+:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Directions"
                                                                           message:@"Select mode of transportation:"
                                                                    preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *drivingAction = [UIAlertAction actionWithTitle:@"Driving" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // this block runs when the driving option is selected
    }];
    UIAlertAction *walkingAction = [UIAlertAction actionWithTitle:@"Walking" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // this block runs when the walking option is selected
    }];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:drivingAction];
    [alert addAction:walkingAction];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
Share:
26,857
user2686101
Author by

user2686101

Updated on July 20, 2022

Comments

  • user2686101
    user2686101 almost 2 years

    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

  • May Phyu
    May Phyu about 7 years
    Hi @Suragch, I try your code. It works for me in iPad and iPod. But, Cancel button does not appear in iPad. I don't know why. It shows in iPod and I use not button. I use image and add tap function on it. So, my code is alertController.popoverPresentationController?.sourceView = self.imgPlay and alertController.popoverPresentationController?.sourceRect = self.imgPlay.bounds .
  • Mansuu....
    Mansuu.... almost 7 years
    It works very fine, but can we customise buttons text @Suragch