【问题标题】:UIAlertController background color iOS10UIAlertController 背景颜色 iOS10
【发布时间】:2016-10-04 12:13:11
【问题描述】:

我为 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 显示为警报。我可以将它作为子视图数组中的 firstObject 访问。现在,为什么发送背景颜色的消息不再起作用了?有人知道一些新的解决方案吗?

【问题讨论】:

  • 谢谢你的回答,但这里的解释是一样的,从子视图数组访问内容视图。它现在显示黄色模糊颜色。非原创。

标签: ios objective-c uiviewcontroller ios10 uialertcontroller


【解决方案1】:

对于遇到同样问题的每个人,我都找到了解决方案:

UIAlertController.view 包含一个子视图,也就是只有一个容器

该子视图包含包含两个它自己的子视图的子视图一个是容器,另一个是用于模糊它的层。

因此,它需要 for in 循环遍历这两个子视图并更改两者的背景颜色。

完整代码:

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
    }

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我用这个:

          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,更快的方式
      • 我已经找到了解决方案,请检查我自己的答案,如果您遇到同样的问题,它会有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多