In my project, I am using UIActionSheet
for displaying for some sorting type. I want to change the color of the text displayed in the action sheet buttons. How can we change the text color?
问题:
回答1:
iOS 8: UIActionSheet
is deprecated. UIAlertController
respects -[UIView tintColor]
, so this works:
alertController.view.tintColor = [UIColor redColor];
Better yet, set the whole window's tint color in your application delegate:
self.window.tintColor = [UIColor redColor];
iOS 7: In your action sheet delegate, implement -willPresentActionSheet:
as follows:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
}
回答2:
In iOS 8, none of this works anymore. UIActionSheet text seems to get its color from window.tintColor.
回答3:
This works for me in iOS8
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];
回答4:
If ever you want to go through the UIActionSheet
's subviews, you should not directly set the text color of the UILabel
but use the UIButton
setTitleColor:forState
method instead. Otherwise, the initial color will be set back upon events like UIControlEventTouchDragOutside for example.
Here is the proper way to do it, reusing the jrc's code:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
}
回答5:
May be a late answer but I figure it could help someone.
iOS 8: You could simply initialize your UIAlertAction with the style: UIAlertActionStyleDestructive like so:
UIAlertAction *delete = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
// your code in here
}];
This will make the button's text red by default.
回答6:
@jrc solution works, but titleLabel color changes when you tap the button. Try this to keep the same color at all states.
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
UIColor *customTitleColor = [UIColor greenColor];
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
[button setTitleColor:customTitleColor forState:UIControlStateHighlighted];
[button setTitleColor:customTitleColor forState:UIControlStateNormal];
[button setTitleColor:customTitleColor forState:UIControlStateSelected];
}
}
}
回答7:
EDIT
Here is a similar question: How to customize Buttons in UIActionSheet?
Try to use the appearance
protocol [NOT WORKING]
UIButton *btn = [UIButton appearanceWhenContainedIn:[UIActionSheet class], nil];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
回答8:
use this for ios 8 and above
SEL selector = NSSelectorFromString(@"_alertController");
if ([actionSheet respondsToSelector:selector])
{
UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];
if ([alertController isKindOfClass:[UIAlertController class]])
{
alertController.view.tintColor = [UIColor blueColor];
}
}
else
{
// use other methods for iOS 7 or older.
}
回答9:
Soulution for Swift 3. Changes all colors.
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .yellow
回答10:
UIAppearance can handle this:
[[UIButton appearanceWhenContainedIn:[UIActionSheet class], nil] setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
Much easier than going through the subviews, but I haven't tested this solution on iOS 6 or earlier.