Change tint color of UIAlertview and UIActionsheet

2019-04-23 15:32发布

问题:

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:

回答1:

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.



回答2:

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

[[UIView appearance] setTintColor:[UIColor whiteColor]];


回答3:

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];
        }
    }
}


回答4:

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.



回答5:

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



回答6:

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.



回答7:

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];