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?
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];
}
}
}
In iOS 8, none of this works anymore. UIActionSheet text seems to get its color from window.tintColor.
UIAlertViews
and UIAlertController
appear in whiteColor
. –
Fluviatile This works for me in iOS8
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];
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];
}
}
}
To change particular buttons colour in alert action use this
let cancelAction = UIAlertAction(title: "Success", style: .cancel, handler: nil)
cancelAction.setValue(UIColor.green, forKey: "titleTextColor")
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.
Soulution for Swift 3. Changes all colors.
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .yellow
@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];
}
}
}
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];
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.
}
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.
In Swift smth like this:
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
actionSheet.view.tintColor = UIColor.primary
© 2022 - 2024 — McMap. All rights reserved.