Objective C UIActionSheet自定义按钮颜色

时间:2014-06-10 09:26:58

标签: objective-c ios7 uiactionsheet

我试图更改UIActionSheet中按钮的颜色。问题(一个非常奇怪的问题)是,如果设备的方向是横向,按钮颜色不会改变! 一些截图: Portrait Landscape

我无法弄明白为什么!

以下是代码:

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

我也尝试过注释过的代码,两者都在Portrait中工作,但不在Landscape中工作!

顺便说一下,在横向和纵向中执行代码!

如果您想在此处查看所有代码,请link

提前谢谢!

2 个答案:

答案 0 :(得分:4)

您可以对UIActionSheet类进行子类化并实现tableView:willDisplayCell:forRowAtIndexPath:选择器:

@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

答案 1 :(得分:1)

使用此方法设置按钮标题颜色,然后我确定您的问题将解决

[[[actionSheet valueForKey:@"_buttons"] objectAtIndex:0] setTitleColor:[[MSAppDelegate sharedInstance] defaultColor] forState:UIControlStateNormal];