如何设置UIActionSheet按钮颜色

时间:2014-05-02 10:46:01

标签: ios objective-c uiactionsheet

我一直在搜索互联网并尝试不同的东西,但似乎无法更改我的操作表上的按钮标题颜色。

这是行动表代码。

- (void)showActionSheet
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"Select Country"
                                  delegate:self
                                  cancelButtonTitle:nil
                                  destructiveButtonTitle:nil
                                  otherButtonTitles:nil];

    for (NSString *listCountry in resultSet) [actionSheet addButtonWithTitle:listCountry];

    [actionSheet showInView:[self.view window]];
}

我已经尝试过这个代理,但它不起作用。

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            button.titleLabel.textColor = [UIColor greenColor];
        }
    }
}

2 个答案:

答案 0 :(得分:0)

确保设置 UIActionSheet委托并在此方法上设置断点, 的 willPresentActionSheet 下,  检查这个方法是否被调用。

如果您设置了委托方式,请尝试使用

- (void) changeTextColorForUIActionSheet:(UIActionSheet*)actionSheet {
    UIColor *tintColor = [UIColor redColor];

    NSArray *actionSheetButtons = actionSheet.subviews;
    for (int i = 0; [actionSheetButtons count] > i; i++) {
        UIView *view = (UIView*)[actionSheetButtons objectAtIndex:i];
        if([view isKindOfClass:[UIButton class]]){
            UIButton *btn = (UIButton*)view;
            [btn setTitleColor:tintColor forState:UIControlStateNormal];

        }
    }
}

更新:

  1. https://github.com/seivan/SHActionSheetBlocks
  2. https://github.com/ianb821/IBActionSheet

答案 1 :(得分:0)

您必须实施UIActionSheetDelegate,然后才能使用willPresentActionSheet委托方法

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
}