UIAlertController background color iOS10

↘锁芯ラ 提交于 2019-12-03 03:47:20

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
    }

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
    }

I use this:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert!"
                                                                       message:@"Message of alert"
                                                                 preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Ok" 
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {

                                                                  //Your code
                                                              }];
        [alert addAction:defaultAction];

        UIView * topview = alert.view.subviews.firstObject;
        UIView * colorView = topview.subviews.firstObject;
        colorView.backgroundColor = [UIColor yellowColor];
        colorView.layer.cornerRadius = 10;
        [self presentViewController:alert animated:YES completion:nil];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!