How to customize Buttons in UIActionSheet?
Asked Answered
C

3

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];

}
Cramp answered 4/7, 2011 at 7:3 Comment(3)
Have look to the SO post #1858818Selestina
its concerning changing width and height and that's not my question and I don't wantCramp
Check out the class I created emulating UIActionSheet look: https://mcmap.net/q/412098/-adding-images-to-uiactionsheet-buttons-as-in-uidocumentinteractioncontrollerOcker
C
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

Chil answered 25/3, 2012 at 5:7 Comment(3)
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)Barbiebarbieri
Does this make it through the app store rejection process?Sepia
Instead of [NSString stringWithFormat:@"%@", [sub class]]; which needs to allocate buffers, etc. use NSStringFromClass([sub class]) - it's more efficient.Hurdygurdy
L
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

Loehr answered 4/7, 2011 at 7:17 Comment(0)
J
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/

Juieta answered 4/7, 2011 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.