2014-06-10 97 views
0

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

我想不通为什么!

下面的代码:

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

我也尝试了注释代码,在人像都工作,但不是在风景!

顺便说一句无论是在景观和肖像的代码执行!

如果你想看到所有的代码,这里的link

预先感谢您!

+1

这将停止工作作为很快你必须翻阅你的选项。那时所有的选项都会在UItableView中显示出来。 –

+0

你错了,它应该是这样!这不是问题! –

+0

显然它没有,要么你不必问如何解决它。在Landscape中,ActionSheet在一个TableView中以不是纵向显示其按钮。所以你的方法在肖像,但不是在景观。 –

回答

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 
+0

谢谢你的回答,我会尽快尝试,但调试子视图是UIActionSheetButton的一个实例,而不是UITableViewCell! –

1

使用此方法来设置按钮标题颜色的话,我敢肯定你的问题会解决

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

这两种都不适用于风景! –

+0

适合我...取消按钮的背景颜色,backgroundcolor属性更改颜色但不正确 –