How to customize Buttons in UIActionSheet?

前端 未结 3 1856
说谎
说谎 2021-01-03 00:41

I want to change the image of the buttons of UIActionSheet, I have images for each button, and I want each <

相关标签:
3条回答
  • 2021-01-03 01:23

    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/

    0 讨论(0)
  • 2021-01-03 01:32

    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

    0 讨论(0)
  • 2021-01-03 01:46

    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

    0 讨论(0)
提交回复
热议问题