如何在UIActionSheet中自定义按钮?

时间:2011-07-04 07:03:20

标签: iphone ipad uiactionsheet

我想更改image of buttons UIActionSheet imagesbutton button每个image,我希望每个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}}

的初始化
{{1}}

3 个答案:

答案 0 :(得分:3)

我创建了一个子类CustomActionSheet派生的UIActionSheet,并实现了一个名为customizeGUI的方法。

我使用像UIActionSheet这样的CustomActionSheet,除了我必须在willPresentActionSheet委托中调用子类的方法customizeGUI:

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

希望这种方式可以帮助您并为您提供自定义UIActionSheet

的想法

答案 1 :(得分:1)

如果您更改这些尺寸,不仅可能会降低您的应用程序的可用性,而且可能违反Apple的人机界面指南,导致在提交时拒绝您的应用程序。

Here is the example how to use UiActionsheet by Apple

答案 2 :(得分:0)

看起来你只需要创建自己的类。我将研究子类化UIView或UIActionSheet。有这些PSD的东西,你可以得到你需要的图像,等待版权。

编辑:如果您最终创建自己的课程,并且有任何好处,请考虑将其上传到http://cocoacontrols.com/