1

In my iOS 8 application I am using UIActionSheet. I tried to change the color of the button title in willPresentActionSheet delegate method, but it is not recognizing buttons as its subview.

I constructed UIActionSheet like this:

UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Select an Option" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
popup.tag = 2;
[popup addButtonWithTitle:@"Add Tag To Chat"];
[popup addButtonWithTitle:@"Remove Tag From Chat"];
[popup addButtonWithTitle:@"Terminate"];
[popup addButtonWithTitle:@"Cancel"];
[popup showInView:self.view];

Delegate is:

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

Buttons are showing, I can click and action performs. But it says count = 0. Why?

Rashad
  • 11,057
  • 4
  • 45
  • 73
  • Action sheet isn't as customizable as it was in iOS 7. Try any open source one replacement since you are only trying to hack your way which might simply make it crash in further OS versions. – Shubhank Dec 31 '14 at 04:42
  • 1
    Under iOS 8, `UIActionSheet` is implemented with the new `UIAlertController` which is completely different and is not customizable in any way. You need to create or use a custom implementation that supports what you want. – rmaddy Dec 31 '14 at 04:48
  • [This is one of the nicest (and customizable) open source action sheets](https://github.com/fastred/AHKActionSheet) – MendyK Dec 31 '14 at 05:15
  • this will help u. Its works for me :) http://stackoverflow.com/questions/24154889/change-text-color-of-items-in-uiactionsheet-ios-8 – RashmiG Apr 02 '15 at 11:45

2 Answers2

1

Try this way, changing the UIColor of subviews:

    UIActionSheet * action = [[UIActionSheet alloc]
                              initWithTitle:@"Title"
                              delegate:self
                              cancelButtonTitle:@"Cancel"
                              destructiveButtonTitle:nil
                              otherButtonTitles:@"",nil];
[[[action valueForKey:@"_buttons"] objectAtIndex:0] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

I've tried it. It is working...

Kanan Vora
  • 224
  • 1
  • 9
-2

The delegate methods in which you're trying to check the number of subviews: willPresentActionSheet seems inappropriate.

willPresentActionSheet is called before the action sheet actually gets rendered.

If you check the number of subviews in didlPresentActionSheet, that will be more appropriate.

Hope this helps..

Rashad
  • 11,057
  • 4
  • 45
  • 73
Henit Nathwani
  • 442
  • 3
  • 10
  • In iOS 8, UIACtionSheet's behavior is similar to that of UIActivityViewController – Henit Nathwani Jan 01 '15 at 07:25
  • Didn't you read the question? I mention "In my iOS 8 application" in the very first line. I just wanted to know if there is any other way I could do it. – Rashad Jan 01 '15 at 08:49