【问题标题】:How to customize Buttons in UIActionSheet?如何自定义 UIActionSheet 中的按钮?
【发布时间】:2011-07-04 07:03:20
【问题描述】:

我想更改UIActionSheetimage ofbuttons,每个button 都有images,我希望每个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];

}

【问题讨论】:

标签: iphone ipad uiactionsheet


【解决方案1】:

我创建了一个子类 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 的想法

【讨论】:

  • 我不会做这样的事情,因为它可能会在 iOS 更新之间中断,你以后只需要做更多的工作。 (如您所见,您已经有了适用于 iOS 4 和 5 的特殊情况)
  • 这是否通过了应用商店拒绝流程?
  • 而不是[NSString stringWithFormat:@"%@", [sub class]];需要分配缓冲区等使用 NSStringFromClass([sub class]) - 它更有效。
【解决方案2】:

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

Here is the example how to use UiActionsheet by Apple

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多