UIAlertController 标题和消息在 iphone 12 中不显示

Siv*_*iva 8 objective-c ios uialertcontroller xcode13

我用 UIAlertControllerStyleActionSheet 风格显示 UIAlertController 。使用 XCode 13 UIAlertController 标题和消息构建时,在 iPhone 12 上不起作用。它在其他设备上工作。之前在 iPhone 12 上运行的代码相同。

-(void)alertSheetUI{
dispatch_async(dispatch_get_main_queue(), ^{
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Static Title" message:@"Static Message" preferredStyle:UIAlertControllerStyleActionSheet];
    
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Data 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }]];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Data 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }]];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Data 3" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }]];
    
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
        [[[UIApplication sharedApplication] delegate].window.rootViewController dismissViewControllerAnimated:YES completion:^{
        }];
    }]];
    [[[UIApplication sharedApplication] delegate].window.rootViewController presentViewController:actionSheet animated:NO completion:nil];
});}
Run Code Online (Sandbox Code Playgroud)

iPhone 12

在此输入图像描述

iPhone SE 在此输入图像描述

视图层次结构将标题和消息显示为白色。所以只有标题和消息不可见。我尝试使用以下代码将标题和消息设置为绿色使用属性文本。但标题和消息颜色在其他设备上也不会更新。其他设备显示为灰色。

更新代码

-(void)alertSheetUI{

dispatch_async(dispatch_get_main_queue(), ^{

        NSString * string = @"Static Title";
        NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:@"Static Title"];
        [attrString addAttribute:NSForegroundColorAttributeName
                                 value:[UIColor greenColor] range:NSMakeRange(0, [string length])];
        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Static Title" message:@"Static Message" preferredStyle:UIAlertControllerStyleActionSheet];
        [actionSheet setValue:attrString forKey:@"attributedTitle"];
        [actionSheet setValue:attrString forKey:@"attributedMessage"];

        [actionSheet addAction:[UIAlertAction actionWithTitle:@"Data 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }]];
        [actionSheet addAction:[UIAlertAction actionWithTitle:@"Data 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }]];
        [actionSheet addAction:[UIAlertAction actionWithTitle:@"Data 3" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }]];

        [actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

            [[[UIApplication sharedApplication] delegate].window.rootViewController dismissViewControllerAnimated:YES completion:^{
            }];
        }]];
        [[[UIApplication sharedApplication] delegate].window.rootViewController presentViewController:actionSheet animated:NO completion:nil];
    });}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Pti*_*Xav 0

您的代码确实要求 uiapplication 显示警报。您可以尝试使用:

 dispatch_async(dispatch_get_main_queue(), ^{
    [self presentViewController:actionShert animated:YES completion:nil];
});
Run Code Online (Sandbox Code Playgroud)

仅在显示或呈现警报时才需要主异步队列。