How to customize UIActionSheet? iOS

caribbean picture caribbean · Jul 30, 2013 · Viewed 33.4k times · Source

Is it possible to create an ActionSheet that have a label between two buttons like this image?

enter image description here

Answer

Max picture Max · Jul 30, 2013

EDIT 2018

I posted this code all the way back in iOS4 when it was potentially useful. iOS Development has grown staggeringly since then, please do not use this code unless you are writing an app for iOS4 for whatever reason! Please refer to the wonderful 3rd party library XLR Action Controller for your mondern actionsheet needs!

It is most certainly possible, and I like to do this all the time!

First, make a @property of an UIActionSheet (in this example, mine is called aac-- this comes in handy especially if you want to call it by UITextFields.

Next, in the method you bring up the actionsheet, say a -(IBAction)userPressedButton:(id)sender, create a local instance of the actionsheet.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

then add it to your property: self.aac = actionsheet

Now you basically have full reign to do anything in this ActionSheet, I will give you an abbreviated form of what I did for a recent project:

- (IBAction)sendDataButtonPressed:(id)sender {

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    self.aac = actionSheet;

    UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"actionsheet_bg.png"]];
    [background setFrame:CGRectMake(0, 0, 320, 320)];
    background.contentMode = UIViewContentModeScaleToFill;
    [self.aac addSubview:background];

    UIButton *cancelButton = [UIButton buttonWithType: UIButtonTypeCustom];
    cancelButton.frame = CGRectMake(0, 260, 320, 50);
    [cancelButton setBackgroundImage:[UIImage imageNamed:@"actionsheet_button.png"] forState: UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonClicked:)    forControlEvents:UIControlEventTouchUpInside];
    cancelButton.adjustsImageWhenHighlighted = YES;
    [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorWithRed:0/255.0f green:177/255.0f blue:148/255.0f alpha:1.0f] forState:UIControlStateNormal];
    cancelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    cancelButton.titleLabel.font = [UIFont fontWithName: @"SourceSansPro-Light" size: 25];

    [self.aac addSubview: cancelButton];

    UIButton *emailResultsButton = [UIButton buttonWithType: UIButtonTypeCustom];
    emailResultsButton.frame = CGRectMake(25, 12, 232, 25);
    [emailResultsButton addTarget:self action:@selector(emailResultsTapped:) forControlEvents:UIControlEventTouchUpInside];
    emailResultsButton.adjustsImageWhenHighlighted = YES;
    [emailResultsButton setTitle:@"Email Results" forState:UIControlStateNormal];
    [emailResultsButton setTitleColor:[UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f] forState:UIControlStateNormal];
    [emailResultsButton setTitleColor:[UIColor colorWithRed:0/255.0f green:177/255.0f blue:148/255.0f alpha:1.0f] forState:UIControlStateHighlighted];
    emailResultsButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    emailResultsButton.titleLabel.font = [UIFont fontWithName: @"SourceSansPro-Light" size: 20];
    [self.aac addSubview: emailResultsButton];

// lots of other buttons...

// then right at the end you call the showInView method of your actionsheet, and set its counds based at how tall you need the actionsheet to be, as follows:

[self.aac showInView:self.view];
[self.aac setBounds:CGRectMake(0,0,320, 600)];

Here is a picture of what happens (remember I omitted all the other buttons in the code example, but they are pictured here): enter image description here

Now, if you want to dismiss the actionSheet -- depending on how you set up your button structure, or perhaps use a UIToolBar (very common) -- you can then in the button's selector action, do this:

-(void)cancelButtonClicked:(id)sender { [self.aac dismissWithClickedButtonIndex:0 animated:YES]; }

Also, just FYI, getting all the dimensions just right for your layout will take a little time, as you aren't working with the view of your main UIView of your UIViewController, rather the view of the actionSheet, so it has different dimensions.

One trick I do is layout the actionsheet in Storyboard with a UIView, then place all the objects you want in your "actionsheet" in that UIView, and you should get accurate dimensions for all the images.

Let me know if you need clarification, good luck!