在iOS 8上运行我们的应用程序时,我遇到了与UIAlertView相关的问题。我显示了一个没有标题的警告框。在iOS 7中一切都很正常,但现在UI看起来很奇怪。
我在这里附上了截图。
我发现的一个解决方案是:当我提供空字符串“”时,它看起来还不错。请参见下面的屏幕截图。但我不确定我提到的问题是否是beta iOS 8版本中的错误,或者是否存在其他更好的解决方案。即使有解决方案,也不像在iOS 7中那样精确。
iOS 7- 显示标题为空的警告框。截图请见此处。
在iOS 8上运行我们的应用程序时,我遇到了与UIAlertView相关的问题。我显示了一个没有标题的警告框。在iOS 7中一切都很正常,但现在UI看起来很奇怪。
我在这里附上了截图。
我发现的一个解决方案是:当我提供空字符串“”时,它看起来还不错。请参见下面的屏幕截图。但我不确定我提到的问题是否是beta iOS 8版本中的错误,或者是否存在其他更好的解决方案。即使有解决方案,也不像在iOS 7中那样精确。
iOS 7- 显示标题为空的警告框。截图请见此处。
在iOS 8上,我能做到的最接近方法是设置标题而不是消息:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location field required." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
需要注意的是,在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];
我用这两种方法得到了相同的结果。
UIAlertView
和UIActionSheet
,我最好的做法是使用initWithTitle:""
,因为在那个时候,当我使用initWithTitle:nil
时,我遇到了设计问题。我试图找回来,但我找不到确切的原因。
从您在iOS 8上的屏幕截图中,我认为UIAlertView
的视图层次结构发生了变化。我认为自动布局
也可能实现在视图层次结构上,因为您可以看到messageLabel跳到titleLabel。
我不能确定,因为UIAlertView
的视图层次结构是私有的。
UIAlertView
类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。
但是,我使用以下代码:
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
。
我通过以下方式在不使用粗体字的情况下获得了良好的消息对齐:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
message:@"\nLocation field required."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
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];