Search code examples
objective-cios7uiactionsheet

Objective C UIActionSheet custom button color


I'm trying to change the color of the button in my UIActionSheet. The problem (a really weird problem) is that if the orientation of the device is Landscape the button color doesn't change! Some screenshot: Portrait Landscape

I can't figure out why!

Here's the code:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
[actionSheet.subviews enumerateObjectsUsingBlock:^(id _currentView, NSUInteger idx, BOOL *stop) {
    if ([_currentView isKindOfClass:[UIButton class]]) {
        [((UIButton *)_currentView).titleLabel setTextColor:[[MSAppDelegate sharedInstance] defaultColor]];
    }
}];
/*
for (UIView *subview in actionSheet.subviews) {
    if ([subview isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)subview;
        [button setTitleColor:[[MSAppDelegate sharedInstance] defaultColor] forState:UIControlStateNormal];
        [button setTitleColor:[[MSAppDelegate sharedInstance] defaultColor] forState:UIControlStateSelected];
    }
    if ([subview isKindOfClass:[UILabel class]]) {
        UILabel *label = (UILabel *)subview;
        label.textColor = [[MSAppDelegate sharedInstance] defaultColor];
    }
}
 */
}

I also tried the commented code, both work in Portrait but not in Landscape!

By the way either in Landscape and in Portrait the code is executed!

If you want to see all the code here's the link

Thank you in advance!


Solution

  • You could Subclass the UIActionSheet class and implement the tableView:willDisplayCell:forRowAtIndexPath: selector:

    @interface MSActionSheet : UIActionSheet
    @end
    
    @implementation MSActionSheet
    
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        [[cell subviews] enumerateObjectsUsingBlock:^(id v, NSUInteger idx, BOOL *stop) {
            [[v subviews] enumerateObjectsUsingBlock:^(id v2, NSUInteger idx, BOOL *stop) {
                if ([v2 isKindOfClass:[UILabel class]]) {
                    [(UILabel *)v2 setTextColor:[[MSAppDelegate sharedInstance] defaultColor]];
                }
            }];
        }];
    }
    
    @end