6

I want to change the image of the buttons of UIActionSheet, I have images for each button, and I want each button show the image I want. I searched the web I found a lot of answers but they didn't work.

Here is the initialization of the UIActionSheet

-(IBAction)showActionSheet:(id)sender {

    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:nil otherButtonTitles:@"Other Button 1", @"Other Button 2", nil];
    popupQuery.actionSheetStyle = UIActionSheetStyleDefault;
    [popupQuery showInView:self.view];
    [popupQuery release];

}
Girish
  • 5,094
  • 4
  • 33
  • 53
user784625
  • 1,838
  • 5
  • 24
  • 38

3 Answers3

3

I create a subclass CustomActionSheet derived UIActionSheet, and implement a method called customizeGUI.

I use CustomActionSheet like UIActionSheet, except I must call method customizeGUI of subclass in willPresentActionSheet delegate:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    if ([actionSheet isKindOfClass:[CustomActionSheet class]]) {
        CustomActionSheet *sheet = (CustomActionSheet*)actionSheet;
        [sheet customizeGUI];
    }
}


(CustomActionSheet.m)

- (void)customizeGUI {
    UIImageView *imgvwBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_actionsheet.png"]];
    CGRect rect = self.bounds;
    imgvwBackground.frame = rect;
    [self insertSubview:imgvwBackground atIndex:0];
    [imgvwBackground release];

    int buttonIndex = 0;
    UIImage *imgButtonDestructive = [UIImage imageNamed:@"btn_actionsheet_destructive.png"];
    UIImage *imgButtonCancel = [UIImage imageNamed:@"btn_actionsheet_cancel.png"];
    UIImage *imgButtonNormal = [UIImage imageNamed:@"btn_actionsheet_normal.png"];
    for (UIView *sub in self.subviews) {
        NSString *className = [NSString stringWithFormat:@"%@", [sub class]];
        if ( ([className isEqualToString:@"UIThreePartButton"]) //iOS 4
            || ([className isEqualToString:@"UIAlertButton"]) ) { //iOS 5
            rect = sub.frame;
            sub.hidden = YES;

            UIButton *btn = [[UIButton alloc] initWithFrame:rect];
            UIImage *imgForButton = nil;
            if (buttonIndex == self.cancelButtonIndex) {
                imgForButton = imgButtonCancel;
            }
            else if (buttonIndex == self.destructiveButtonIndex) {
                imgForButton = imgButtonDestructive;
            }
            else {
                imgForButton = imgButtonNormal;
            }
            NSString *sTitle = [self buttonTitleAtIndex:buttonIndex];
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:19];
            [btn setBackgroundImage:imgForButton forState:UIControlStateNormal];
            [btn setTitle:sTitle forState:UIControlStateNormal];
            btn.tag = buttonIndex;
            [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

            [self addSubview:btn];
            [btn release];

            buttonIndex++;
        }
    }
}

- (void)buttonClicked:(id)sender {
    UIButton *btn = sender;
    if ([self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
        [self.delegate actionSheet:self clickedButtonAtIndex:btn.tag];
    }
    [self dismissWithClickedButtonIndex:btn.tag animated:YES];
}

Hope this way can help you and give you an idea to customize UIActionSheet

Cao Huu Loc
  • 1,479
  • 15
  • 16
  • I would stay away from doing something like this, as it'll likely break between iOS updates and you'll just have to do more work later. (as you can see, you already have a special cases for iOS 4 and 5) – Aaron Ash Jun 24 '12 at 20:29
  • 2
    Does this make it through the app store rejection process? – Kaolin Fire Nov 01 '12 at 00:59
  • Instead of [NSString stringWithFormat:@"%@", [sub class]]; which needs to allocate buffers, etc. use NSStringFromClass([sub class]) - it's more efficient. – Charlie Monroe Feb 24 '14 at 17:16
1

If you change these sizes, not only might you make your application less usable, but that may violate Apple's Human Interface Guidelines, leading to a rejection of your application when submitted.

Here is the example how to use UiActionsheet by Apple

Rakesh Bhatt
  • 4,600
  • 3
  • 23
  • 38
0

Looks like you'll just have to create your own class. I'd look into subclassing either UIView or UIActionSheet. There are those PSD things around that you can get the images you'll need, pending copyright.

EDIT: If you do end up creating your own class, and its any good, consider uploading it to http://cocoacontrols.com/

Sam Jarman
  • 7,025
  • 14
  • 49
  • 98