UIAlertController背景色iOS10

UIAlertController背景色iOS10,ios,objective-c,uiviewcontroller,ios10,uialertcontroller,Ios,Objective C,Uiviewcontroller,Ios10,Uialertcontroller,我为iOS9编写的代码运行得非常好: UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Select source" message:nil pref

我为iOS9编写的代码运行得非常好:

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;
我的观点是,
UIAlertController
继承自
UIViewController
,并且
UIViewController
具有可以更改的属性
UIView
。这是有效的。现在,从
UIViewController
继承的视图有了自己的子视图,即contentView,显示为警报。我可以作为子视图数组中的第一个对象访问它。现在,为什么发送背景色的消息不再有效?有人知道一些新的解决方案吗?

我使用这个:

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

对于所有遇到相同问题的人,我找到了解决方案:

UIAlertController.view
包含一个子视图,这是唯一的容器

该子视图包含包含两个子视图的子视图,一个是container,另一个是用于模糊它的层

所以,它需要在循环中迭代这两个子视图,并更改这两个子视图的背景色

完整代码:

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
    }
在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
    }

抱歉,但方法与我的示例代码非常相似。我试过了,它显示黄色,但模糊,我需要原始的。然后我建议你实现你的自定义uialertcontroller,更快的方式我找到了解决方案,检查我自己的答案,如果你遇到同样的问题可能会有所帮助。谢谢你的回答,但这里的解释是一样的,从子视图数组访问内容视图。它现在显示黄色模糊的颜色。不是原创的。