Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the background color of the UIAlertController?

Due to strange behavior of UIActionSheet in iOS 8, I have implemented UIAlertController with UIAction as buttons in it. I would like to change the entire background of the UIAlertController. But I can't find any ways to do it.

Tried even with,

actionController.view.backgroundColor = [UIColor blackColor]; 

But didn't help me out. Any inputs on this regard will be appreciable.

Thanks in advance.

like image 237
GJDK Avatar asked Nov 11 '14 11:11

GJDK


People also ask

How do you change the background color on a storyboard?

First let us see using storyboard, Open Main. storyboard and add one view to the View Controller. On the right pane you can see the property, and from there update the background color to color you want your view to be as show below.

How do I change the background in Xcode?

At the top select the attributes inspector. Under the section "View" there should be a spot that says "Background". click that and choose your colour.


1 Answers

You have to step some views deeper:

let subview = actionController.view.subviews.first! as UIView let alertContentView = subview.subviews.first! as UIView alertContentView.backgroundColor = UIColor.blackColor() 

And maybe you want to keep original corner radius:

 alertContentView.layer.cornerRadius = 5; 

Sorry for the "Swifting" but i'm not familiar with Objective-C. I hope that's similar.

Of course it's also important to change the title color of the actions. Unfortunately I don't know, how to set the color of actions separately. But this is, how you change all button text colors:

actionController.view.tintColor = UIColor.whiteColor(); 

EDIT:

The corner radius of the UIAlertController has changed since this answer's been posted! Replace this:

 alertContentView.layer.cornerRadius = 5; 

to this:

 actionContentView.layer.cornerRadius = 15 
like image 197
nischtname Avatar answered Sep 20 '22 16:09

nischtname