【发布时间】:2013-10-06 18:27:19
【问题描述】:
我在整个应用程序中使用绿色作为“动作颜色”,并且我希望 UIActionSheets 中的选项也为绿色,以保持一致性。如何将 UIActionSheet 选项的颜色从蓝色更改为绿色?
【问题讨论】:
标签: ios objective-c ios7 uiactionsheet
我在整个应用程序中使用绿色作为“动作颜色”,并且我希望 UIActionSheets 中的选项也为绿色,以保持一致性。如何将 UIActionSheet 选项的颜色从蓝色更改为绿色?
【问题讨论】:
标签: ios objective-c ios7 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];
}
}
}
【讨论】:
button.titleLabel.textColor = MY_COLOR;button.titleLabel.font = MY_FONT; 时,选项已更改为新字体,但仍保持蓝色和红色。只需将顺序更改为 button.titleLabel.font = MY_FONT;button.titleLabel.textColor = MY_COLOR; 即可解决问题,我得到了新字体和新颜色。
你可以这样做:
// 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]];
希望对你有帮助:)
【讨论】: