UIAlertview and UIActionsheet Button Color Changes

I am trying to adapt my application for iOS 7. The problem I am encountering is that I cannot change the color of the shades of some controls.

I added

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

for my application delegate

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

This basically helped, but the color of the message box and worksheet buttons still remains blue by default.

How can I recolor all such buttons?

Some screenshots:

iOS7 message boxiOS7 action sheet

+9
ios objective-c cocoa-touch ios7 uialertview uiactionsheet
source share
7 answers

Since UIAlertView deprecated, you can. Use the UIAlertController .

You can use the tintColor property.

OLD

The UIAlertView class is intended to be used as is, not subclass support. The presentation hierarchy for this class is private and should not be changed.

- from Apple Doc

You can use the tintColor property, or for this you can use some custom library, you can find it at cocoacontrols.com.

+11
source share

I managed to change the cancel button text color to white in the application delegate.

 [[UIView appearance] setTintColor:[UIColor whiteColor]]; 
+10
source share

For the action bar, you can use

Use the delegate option willPresentActionSheet of the UIActionSheet delegate to change the color of the action bar button.

 - (void)willPresentActionSheet:(UIActionSheet *)actionSheet { for (UIView *subview in actionSheet.subviews) { if ([subview isKindOfClass:[UIButton class]]) { UIButton *button = (UIButton *)subview; button.titleLabel.textColor = [UIColor greenColor]; } } } 
+8
source share

You can adjust the color by searching for and changing the UILabel in the subtask hierarchy of the warning window, which is created immediately after the warning is displayed:

 - (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 in a UIView that returns an array of views in the full subordination hierarchy of a given class or subclass.

+1
source share

for UIAlertView with colored buttons you can use 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

+1
source share

Combination of the best answers above and update for obsolescence:

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

or Swift:

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

Opened in 2018, Swift 4 / iOS 12.

0
source share

In iOS 6.0, create a custom view in the application’s deletion

 .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]; 
-3
source share

All Articles