简体   繁体   English

如何设置UIActionSheet按钮的颜色

[英]How to set UIActionSheet button color

I've been searching the internet and trying different thing but cannot seem to change the button title colour on my action sheet. 我一直在互联网上搜索并尝试其他操作,但似乎无法更改操作表上的按钮标题颜色。

Here is the action sheet code. 这是操作表代码。

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

I've tried this delegate but it does not work. 我已经尝试过此委托,但它不起作用。

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

Make sure you set, UIActionSheet delegate and set breakpoint on this method, willPresentActionSheet , check whether this method getting called . 确保设置UIActionSheet委托并在此方法上设置断点willPresentActionSheet ,检查是否调用了此方法。

If you have set delegate means, try this one 如果已设置委托方式,请尝试此方法

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

        }
    }
}

Update: 更新:

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

You must implement UIActionSheetDelegate and then you can use willPresentActionSheet delegate method 您必须实现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];
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM