Change tint color of UIAlertview and UIActionsheet buttons
Asked Answered
F

7

9

I am trying to adapt my application for iOS 7. The issue I am having is I can not change the tint color of some controls.

I did add

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if (IOS7_OR_LATER)
    self.window.tintColor = [self greenTintColor];

to my app delegate's

           - (BOOL)application:(UIApplication *)application
 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

It mostly helped but color of message box and action sheet buttons is still the default blue.

How can I recolor all such buttons too?

Some screenshots:

iOS7 message box iOS7 action sheet

Frumenty answered 15/11, 2013 at 6:38 Comment(0)
R
11

As UIAlertView is deprecated You can. Use UIAlertController.

You can use tintColor property.

OLD

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

-From Apple Doc

You can use tintColor property or You can use Some Custom Library for that, you can find it at cocoacontrols.com.

Rideout answered 15/11, 2013 at 6:43 Comment(2)
In iOS 8 there is alert.view.tintColor = UIColor.redColor()Dorolisa
Answer has been copied from https://mcmap.net/q/638692/-how-to-change-button-text-color-of-uialertview-in-ios7 ,at least change the text!Karolekarolina
P
11

I was able to change the cancel button's text color to white in app delegate.

[[UIView appearance] setTintColor:[UIColor whiteColor]];
Pointer answered 3/2, 2015 at 21:23 Comment(3)
Saved my day! Must be in answer!Humanitarianism
Well, it does more than it should. I would rather use [[UIView appearanceWhenContainedIn:[UIAlertView class], nil] setTintColor:[UIColor whiteColor]]; and [[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor whiteColor]];Excipient
I'd rather @KévinRenella's answer as well. Ali's answer is very dangerous.Gadwall
E
6

For Actionsheet You can use

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];
        }
    }
}
Eustace answered 15/11, 2013 at 6:50 Comment(2)
However, this change is only temporary. If you tap and hold a button and release the touch outside of it, the color changes back to the default color. In fact it already changes to the default color on touch down.Alms
Call [button setTitleColor:forControlState: instead for it to last.Retaliate
R
3

Combining best answers above, and updated for deprecation:

[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UIAlertController class]]] setTintColor:[UIColor greenColor]];

or Swift:

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .green

Works in 2018, Swift 4 / iOS 12.

Reify answered 30/11, 2018 at 19:38 Comment(0)
H
1

You can adjust the color by searching and modifying the UILabel in the subview hierarchy of the alert window that is created right after showing the alert:

- (void)setButtonColor:(UIColor*)buttonColor {
    dispatch_after(dispatch_time(0,1), dispatch_get_main_queue(), ^{
        NSMutableArray *buttonTitles = [NSMutableArray array];
        for (NSUInteger index = 0; index < self.numberOfButtons; index++) {
            [buttonTitles addObject:[self buttonTitleAtIndex:index]];
        }
        for (UILabel *label in [[[UIApplication sharedApplication] keyWindow] recursiveSubviewsOfKind:UILabel.class]) {
            if ([buttonTitles containsObject:label.text]) {
                label.textColor = buttonColor;
                label.highlightedTextColor = buttonColor;
            }
        }
    });
}

[alert show];
[alert setButtonColor:UIColor.redColor];

The recursiveSubviewsOfKind: method is a category on UIView that returns an array of views in the complete subview hierarchy of the given class or subclass.

Hiramhirasuna answered 8/7, 2014 at 14:44 Comment(1)
Thank you. This works for UIAlertView but there are also UIBarButtonItems, for example. For UIBarButtonItem the effect is temporary: color reverts to default after tap or after pressing and holding the button.Frumenty
L
1

for UIAlertView with colored buttons you can use the cocoapod "SDCAlertView"

about CocoaPods: http://www.cocoapods.org

how to install CocoaPods: https://www.youtube.com/watch?v=9_FbAlq2g9o&index=20&list=LLSyp50_buFrhXC0bqL3nfiw

Linsk answered 23/9, 2014 at 11:35 Comment(1)
Thank you. It looks promising.Frumenty
G
-3

In iOS 6.0 create custom view in App delegate

.h
UIView* _loadingView;
    UIView* _subView;
    UIActivityIndicatorView*loadingIndicator;
    UITabBarController *tabBar_Controller;
    NSTimer *timer;


@property (strong, nonatomic) UIView* _loadingView;
@property (strong, nonatomic) UIView* _subView;



.m- (void)fadeScreen
{
    [UIView beginAnimations:nil context:nil]; // begins animation block
    [UIView setAnimationDuration:3.0]; // sets animation duration
    [UIView setAnimationDelegate:self]; // sets delegate for this block
    [UIView setAnimationDidStopSelector:@selector(finishedFading)];
    self.txtview.alpha = 0.0;  // Fades the alpha channel of this view
    [UIView commitAnimations];  // commits the animation block.  This
}



- (void) finishedFading
{
    [self.txtview removeFromSuperview];
}




- (void)showConnectivity:(NSString *)strTitle
{
    [_loadingView setBackgroundColor:[UIColor clearColor]];
    [_loadingView setAlpha:0.5];
    [_loadingView.layer setCornerRadius:10];
    [self.window addSubview:_loadingView];
    [_loadingView setHidden:NO];

    [_subView.layer setCornerRadius:7];
    [_subView setBackgroundColor:[UIColor colorWithHue:0.0f saturation:0.0f brightness:0.0f alpha:0.6]];
    [_subView setOpaque:YES];
    [self.window addSubview:_subView];
    [_subView setHidden:NO];

    [_loadingView setHidden:NO];
    [_subView setHidden:NO];

    loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [loadingIndicator setFrame:CGRectMake(85,10,35,35)];
    [_subView addSubview:loadingIndicator];
    [loadingIndicator setBackgroundColor:[UIColor redColor]];
    [loadingIndicator startAnimating];

    UILabel *_lab=[[UILabel alloc]initWithFrame:CGRectMake(8,10,72,45)];
    [_lab setText:strTitle];
    [_lab setTextColor:[UIColor whiteColor]];
    [_lab setBackgroundColor:[UIColor clearColor]];
    [_lab setFont:[UIFont boldSystemFontOfSize:13.0]];
    [_lab setTextAlignment:NSTextAlignmentCenter];
    [_subView addSubview:_lab];


}

- (void)CoonectingViewHidden
{

    [_loadingView setHidden:YES];
    [_subView setHidden:YES];

    NSArray *_aryViews = [_subView subviews];
    for(int i = 0; i<[_aryViews count];i++)
    {
        id obj = [_aryViews objectAtIndex:i];
        if(![obj isKindOfClass:[UIActivityIndicatorView class]])
            [obj removeFromSuperview];
    }
    [loadingIndicator stopAnimating];
    [loadingIndicator hidesWhenStopped];

}



in using .m
#import"Appdelegate.h"

- (void)showLoadingIndicator:(NSString *)message
{
    AppDelegate *delegateObj2=(AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegateObj2 showConnectivity:message];
}
-(void)stopLoading
{
    AppDelegate *delegateObj3=(AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegateObj3 CoonectingViewHidden];

}



// [self showLoadingIndicator:@"Loading"];
n

[self stopLoading];
Gramercy answered 1/2, 2014 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.