【问题标题】:UIAlertController title and message not displaying in iphone 12UIAlertController 标题和消息未在 iphone 12 中显示
【发布时间】:2021-12-29 06:23:54
【问题描述】:

我用 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];
});}

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

【问题讨论】:

  • 用模拟器试一次
  • 运行设备的系统版本是多少?图片似乎您已经自定义了 UIAlertController 的外观?
  • @Anbu.Karthik 在 iPhone 12 模拟器上运行良好
  • @childrenOurFuture 设备版本是 iOS 15。我没有自定义 UIAlertController UI。看起来这仅在 iPhone 12 上使用提到的代码。已添加 iPhone SE 屏幕截图以供参考。
  • Tint 颜色/UIAppearance 自定义完成了吗?文字在白色背景上看起来是“白色的”,所以不可见。如果您检查(例如使用调试视图层次结构),文本存在,对吗?

标签: ios objective-c uialertcontroller xcode13


【解决方案1】:

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

 dispatch_async(dispatch_get_main_queue(), ^{
    [self presentViewController:actionShert animated:YES completion:nil];
});

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

这个问题也有不同的答案here

【讨论】:

  • 在库中创建的屏幕。我没有自我对象。所以你的答案不适用于我的场景
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多