更改UIAlertController按钮背景颜色

时间:2018-01-02 12:51:44

标签: ios objective-c xcode uialertcontroller

是否可以更改UIAlertController的背景颜色?我设法改变了UIAlertView的背景颜色。我也经历过第三方同样的事情,但我想要在不使用任何第三方的情况下这样做。

以下是我的代码:

UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"Title"
                                                                  message:@"Message"
                                                           preferredStyle:UIAlertControllerStyleAlert];
    UIView *firstSubview = alert.view.subviews.firstObject;
    UIView *alertContentView = firstSubview.subviews.firstObject;
    for (UIView *subSubView in alertContentView.subviews) {
        subSubView.backgroundColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
    }
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
        //Close Action
    }];

    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
        //Close Action
    }];

    [cancelAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];
    [deleteAction setValue:[UIColor redColor] forKey:@"titleTextColor"];

    [alert addAction:cancelAction];
    [alert addAction:deleteAction];
    [self presentViewController:alert animated:YES completion:nil];

提前致谢!!

0 个答案:

没有答案