我在我的应用程序中使用绿色作为“操作颜色”,我希望我的UIActionSheets选项也是绿色的,以保持一致。如何将UIActionSheet选项的颜色从蓝色更改为绿色?
我在我的应用程序中使用绿色作为“操作颜色”,我希望我的UIActionSheets选项也是绿色的,以保持一致。如何将UIActionSheet选项的颜色从蓝色更改为绿色?
使用UIActionSheet
的willPresentActionSheet
委托方法来更改操作表按钮的颜色。
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
button.titleLabel.textColor = [UIColor greenColor];
}
}
}
你可以像这样做:
// Your code to instantiate the UIActionSheet
UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
// Configure actionSheet
// Iterate through the sub views of the action sheet
for (id actionSheetSubview in actionSheet.subviews) {
// Change the font color if the sub view is a UIButton
if ([actionSheetSubview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)actionSheetSubview;
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
}
}
如果您打算经常重复使用这段代码,我建议您创建一个UIActionSheet的子类并使用这段代码。您可以在AppDelegate的didFinishLaunchingWithOptions方法中使用以下简短的函数轻松更改应用程序的色调颜色。
[[UIView appearance] setTintColor:[UIColor redColor]];
button.titleLabel.textColor = MY_COLOR; button.titleLabel.font = MY_FONT;
时,选项已更改为新字体,但仍保持蓝色和红色。只需将顺序更改为button.titleLabel.font = MY_FONT; button.titleLabel.textColor = MY_COLOR;
即可解决问题,我得到了新字体和新颜色。 - Breno Gazzola