在UIAlertView iOS 8 beta 5中没有标题的UI问题
在iOS 8上运行我们的应用程序时,我遇到了与UIAlertView有关的问题。我显示标题为nil的警报。 它在iOS 7中工作正常,但现在UI看起来很奇怪。
我在这里附上截图。
我发现一个解决scheme是,当我提供空string@“”它看起来没问题。 看下面的截图。 但我不知道我提到的问题是testing版iOS 8版本中的错误还是有其他更好的解决scheme。 即使是解决scheme,它也不像iOS 7那样精确。
iOS 7 – 显示标题为nil的警报视图。 这里截图。
对于我来说,使用initWithTitle:@""
是最好的做法initWithTitle:@""
对于iOS 6以来的UIAlertView
, UIActionSheet
因为在使用initWithTitle:nil
时候我遇到了一个devise问题initWithTitle:nil
。 我试图找回来,我找不到原因究竟是什么。
从iOS 8上的屏幕截图中,我认为UIAlertView for iOS 8的视图层次结构发生了变化。我认为Auto layout
可能会在视图hierarachy上实现,并且您可以看到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
。
我能用iOS 8获得的最接近的是设置标题而不是消息:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location field required." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
但是,应该注意的是, UIAlertView
在iOS 8中已经被弃用了,如果你打算为iOS 7和iOS 8使用单独的代码path,你应该使用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];
我用两种方法得到了相同的结果。
我设法得到体面的消息alignment没有粗体的字体:
- 将标题设置为@“”而不是零,并且
- (如果IOS8)在邮件前添加“\ n”。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"\nLocation field required." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];