Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController background color iOS10

Code that I wrote for iOS9, worked really well:

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Select source"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    alert.view.backgroundColor = DARK_BLUE;
    alert.view.tintColor = NEON_GREEN;
    UIView *subview = alert.view.subviews.firstObject;
    UIView *alertContentView = subview.subviews.firstObject;
    alertContentView.backgroundColor = DARK_BLUE;
    alertContentView.layer.cornerRadius = 10;

My point of view is that UIAlertController is inheriting from UIViewController, and UIViewController have property UIView that can be changed. And this is working. Now, that view inherited from UIViewController have it's own subview that is contentView showed as Alert. I can access it as firstObject in array of subviews. Now, why message for sending background color isn't working anymore? Do anyone know some new solution?

like image 606
Stefan Avatar asked Dec 01 '22 16:12

Stefan


2 Answers

For everyone that will bump into the same problem, I've found the solution:

UIAlertController.view contains one subview, that is only container.

That subview contains subview that contains two it's own subviews, one is container, and another is layer for blurring it.

So, it needs for in loop to iterate through that two subviews and change background color of both.

Full code:

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Select source"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    alert.view.tintColor = NEON_GREEN;

    UIView *firstSubview = alert.view.subviews.firstObject;

    UIView *alertContentView = firstSubview.subviews.firstObject;
    for (UIView *subSubView in alertContentView.subviews) { //This is main catch
        subSubView.backgroundColor = DARK_BLUE; //Here you change background
    }
like image 192
Stefan Avatar answered Dec 05 '22 01:12

Stefan


In Swift 3.0

let FirstSubview = alertController.view.subviews.first
    let AlertContentView = FirstSubview?.subviews.first
    for subview in (AlertContentView?.subviews)! {
        subview.backgroundColor = UIColor.black
        subview.layer.cornerRadius = 10
        subview.alpha = 1
        subview.layer.borderWidth = 1
        subview.layer.borderColor = UIColor.yellow.cgColor
    }
like image 22
user2643679 Avatar answered Dec 05 '22 01:12

user2643679