iOS 8 beta 5的UIAlertView中标题为空的UI问题

14

在iOS 8上运行我们的应用程序时,我遇到了与UIAlertView相关的问题。我显示了一个没有标题的警告框。在iOS 7中一切都很正常,但现在UI看起来很奇怪。

我在这里附上了截图。

输入图片描述 我发现的一个解决方案是:当我提供空字符串“”时,它看起来还不错。请参见下面的屏幕截图。但我不确定我提到的问题是否是beta iOS 8版本中的错误,或者是否存在其他更好的解决方案。即使有解决方案,也不像在iOS 7中那样精确。

输入图片描述

iOS 7- 显示标题为空的警告框。截图请见此处。 输入图片描述

4个回答

11

在iOS 8上,我能做到的最接近方法是设置标题而不是消息:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location field required." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

UIAlertView iOS8 title set

需要注意的是,在iOS 8中UIAlertView已经被弃用,如果你要为iOS 7和iOS 8使用分开的代码路径,则应改用UIAlertController

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Location field required."  message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [self dismissViewControllerAnimated:YES completion:nil];
}]];

[self presentViewController:alert animated:YES completion:nil];

我用这两种方法得到了相同的结果。


8
自iOS 6以来,对于UIAlertViewUIActionSheet,我最好的做法是使用initWithTitle:"",因为在那个时候,当我使用initWithTitle:nil时,我遇到了设计问题。我试图找回来,但我找不到确切的原因。

从您在iOS 8上的屏幕截图中,我认为UIAlertView视图层次结构发生了变化。我认为自动布局也可能实现在视图层次结构上,因为您可以看到messageLabel跳到titleLabel。

我不能确定,因为UIAlertView的视图层次结构是私有的。

UIAlertView类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

请参阅:https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html

但是,我使用以下代码:

NSLog(@"%@",[self.alertView description]);

iOS 7.1 上的结果:

<UIAlertView: 0x7fb3c05535b0; frame = (18 263; 284 62); opaque = NO; layer = <CALayer: 0x7fb3c0519810>>

iOS 8.0 上的结果:

<UIAlertView: 0x7bf64840; frame = (0 0; 0 0); layer = <CALayer: 0x7bf648f0>>

我不确定为什么iOS 8中UIAlertView的框架是(0 0; 0 0);

就像Mike所说的那样,我认为你应该学习使用iOS 8的UIAlertController


4

我通过以下方式在不使用粗体字的情况下获得了良好的消息对齐:

  1. 将标题设置为 @"" 而不是 nil,以及
  2. (如果是 IOS8) 在消息前面添加 "\n"。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
                                    message:@"\nLocation field required." 
                                    delegate:nil 
                                    cancelButtonTitle:@"OK" 
                                    otherButtonTitles:nil];

enter image description here


0
请尝试这段代码。 在我的电脑上可以运行。 Xcode 版本为 9.2。
UIAlertController * alert=   [UIAlertController
                              alertControllerWithTitle:nil
                              message:nil
                              preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* btn1 = [UIAlertAction
                       actionWithTitle:@"OKAY"
                       style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action)
                       {

                       }];

[self presentViewController:alert animated:YES completion:nil];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接