I use green as the "action color" throughout my app, and I want the options in my UIActionSheets to be green as well, for consistency. How can I change the colour of the UIActionSheet options to green from blue?
In iOS 7, how do I change the color of the options in my UIActionSheet?
Asked Answered
Utilize the willPresentActionSheet
delegate method of UIActionSheet
to change the action sheet button color.
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
button.titleLabel.textColor = [UIColor greenColor];
}
}
}
This answer is better. Uses a delegate method not to mention more concise. Also the above answer doesn't change the cancel button's color. –
Spradling
For some reason, if you also want to change the font, you have to do that before changing the color, or it won't work –
Loop
@BrenoGazzola: What happens? –
Upcountry
When doing
button.titleLabel.textColor = MY_COLOR;button.titleLabel.font = MY_FONT;
the options were changed to the new font but remained blue and red. Simply changing the order to button.titleLabel.font = MY_FONT;button.titleLabel.textColor = MY_COLOR;
fixed the problem and I got both the new font and new colors. –
Loop That is strange. Which version of iOS are you running and what device/simulator are you using to run the code? If I can replicate this tonight, I'll submit a bug ticket to apple. –
Upcountry
don't forget to set the button's color for selected states as well! #7807340 Regular buttons will use UIControlStateHighlighted when tapped, but the Cancel button will use UIControlStateSelected –
Zeb
You could do something like this:
// 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];
}
}
If you're going to reuse this a lot, I'd subclass UIActionSheet and use this code.
Is that using private APIs? –
Innoxious
No, it is just accessing all the subviews of a UIActionSheet and finding the buttons –
Tibbitts
When you tap it it changes back to blue. –
Innoxious
Did you manage to make it work for the cancel button ? Mine keep geting it's previous color –
Puncheon
You can change easily the application's tint color using this short function on your AppDelegate didFinishLaunchingWithOptions method.
[[UIView appearance] setTintColor:[UIColor redColor]];
Hope it will help you :)
© 2022 - 2024 — McMap. All rights reserved.