Jeyhun Karimov
Jeyhun Karimov

Reputation: 1295

iOS change color of dynamic UIActionsheet buttons

I have made dynamic uiactionsheet like:

- (IBAction)selectCity
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:kCityActionSheetTitle
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];
    for (NSString * cityNameNSS in [_cityNamesAndUrlsNSMD allKeys]) {
        [actionSheet addButtonWithTitle:cityNameNSS];
    }
    [actionSheet addButtonWithTitle:kCancellButtonTitle];
    actionSheet.cancelButtonIndex = [[_cityNamesAndUrlsNSMD allKeys] count];
    actionSheet.tag = 2;
    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];

}

I want to change the uiactionsheet buttons' tint colors. Therefore I wrote:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {

        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            [button setTitleColor:[UIColor colorWithRed:0.0/255.0 green:150.0/255.0 blue:94.0/255.0 alpha:1.0]
                         forState:UIControlStateNormal];
        }
        else if([subview isKindOfClass:[UILabel class]]){
            UILabel *label = (UILabel *)subview;
            [label setTextColor:[UIColor colorWithRed:0.0/255.0 green:150.0/255.0 blue:94.0/255.0 alpha:1.0]];

        }
    }
}

However, only in this case(in dynamic uiactionsheet) , the button colors are not changing. I have other uiactionsheet in the same controller, they are changing their colors.

What can be a possible problem/solution?

Upvotes: 0

Views: 1279

Answers (3)

Kimo Gao
Kimo Gao

Reputation: 115

SEL selector = NSSelectorFromString(@"_alertController");
if ([actionSheet respondsToSelector:selector])
{

    UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];


    if ([alertController isKindOfClass:[UIAlertController class]])
    {
        alertController.view.tintColor = [UIColor redColor];
    }
}
else
{
    // use other methods for iOS 7 or older.
}

Upvotes: 1

Mind Pixel
Mind Pixel

Reputation: 814

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

In your Actionsheet Delegate just write this code.

If you want more information just follow this link : https://github.com/ianb821/IBActionSheet

Upvotes: 0

Rose
Rose

Reputation: 437

Please add a tag to your buttons

 - (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
   for (UIView *subview in actionSheet.subviews)
   {
     if ([subview isKindOfClass:[UIButton class]])
     {
       if (subview.tag == 3)
        {
         NSLog(@"ButtonIndex===%d",subview.tag);
        UIButton *button = (UIButton *)subview;
        [button setTitleColor:[UIColor colorWithRed:0.0/255.0 green:150.0/255.0 blue:94.0/255.0 alpha:1.0]
                     forState:UIControlStateNormal];


        }
      }
  }
}

Upvotes: 1

Related Questions