How to customize Buttons in UIActionSheet?

假如想象 提交于 2019-11-29 20:29:54

问题


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

}

回答1:


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




回答2:


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




回答3:


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/



来源:https://stackoverflow.com/questions/6568308/how-to-customize-buttons-in-uiactionsheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!