簡體   English   中英

UIAlertController背景色iOS10

[英]UIAlertController background color iOS10

我為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顯示為Alert。 我可以在子視圖數組中以firstObject的形式訪問它。 現在,為什么發送背景顏色的消息不再起作用? 有誰知道一些新的解決方案?

對於遇到同樣問題的每個人,我找到了解決方案:

UIAlertController.view包含一個子視圖,它只是容器

該子視圖包含子視圖,其中包含兩個自己的子視圖一個是容器 ,另一個是模糊它的圖層。

因此,它需要在循環中迭代這兩個子視圖並更改兩者的背景顏色。

完整代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM