UIAlertController / UIAlertView在iOS 8上滚动
我想知道是否有人知道UIAlertViews和UIAlertControllers不会在iOS 8上滚动的事实的一个很好的解决scheme? 这里是一个例子:
[[[UIAlertView alloc] initWithTitle:@"Test" message:@"long string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\n long string\nlong string\nlong string\nlong string\nlong string\nlong string\n long string\nlong string\nlong string\nlong string\nlong string\nlong string\n" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
在iOS 7和8上运行该代码会产生以下结果。 (将其更改为UIAlertController没有区别)。
iOS 8:
IOS 7:
正如你可以看到它显然在iOS 7上滚动,但不在iOS 8上。是否有一些属性,我在这里失踪或只是一个testing版的bug?
虽然我不知道正确的方法,但我仍然可以解决这个问题。 看起来像视图滚动如果您设置框架属性(但不是CGRectZero)。 它在iOS 8.0.2上运行良好。
NSString* messageString = @"long string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\n"; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:messageString preferredStyle:UIAlertControllerStyleAlert]; alertController.view.frame = [[UIScreen mainScreen] applicationFrame]; [alertController addAction:[UIAlertAction actionWithTitle:@“OK” style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ [self okButtonTapped]; }]]; [self presentViewController:alertController animated:YES completion:nil]; //—— - (void) okButtonTapped{};
cafedeichi的解决scheme没有为我工作,所以我结束了使用DTAlertView在iOS 8和iOS 7的作品。
这个问题已经在iOS 8.3上解决了我一直在使用cafedeichi解决scheme,但是如果不能在iPhone和iPod touch设备的横向模式下工作,并且如果添加文本框,它将会在iOS 8.3上崩溃,所以我正在使用这个代码现在修复了上面提到的两个问题:
NSString* messageString = @"long string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\nlong string\n"; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:messageString preferredStyle:UIAlertControllerStyleAlert]; if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.3) { CGFloat screenHeight; CGFloat screenWidth; if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortraitUpsideDown){ screenHeight = [UIScreen mainScreen].applicationFrame.size.height; screenWidth = [UIScreen mainScreen].applicationFrame.size.width; } else{ screenHeight = [UIScreen mainScreen].applicationFrame.size.width; screenWidth = [UIScreen mainScreen].applicationFrame.size.height; } CGRect alertFrame = CGRectMake([UIScreen mainScreen].applicationFrame.origin.x, [UIScreen mainScreen].applicationFrame.origin.y, screenWidth, screenHeight); alertController.view.frame = alertFrame; } [alertController addAction:[UIAlertAction actionWithTitle:@“OK” style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ [self okButtonTapped]; }]]; [self presentViewController:alertController animated:YES completion:nil]; //—— - (void) okButtonTapped{};
用这个 :
NSString *message = @"YOUR LONG MESSAGE" ; NSInteger lines = [[message componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] count]; UITextView *txtView = nil ; if (lines > MAX_LINES_TO_SHOW) { txtView = [[UITextView alloc] init]; [txtView setBackgroundColor:[UIColor clearColor]]; [txtView setTextAlignment:NSTextAlignmentCenter] ; [txtView setEditable:NO]; [txtView setText:message]; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] ; [alert setValue:txtView forKey:@"accessoryView"]; [alert setTag:tag] ; [alert show] ;
任何UIAlertView实例必须首先更改为UIAlertController实例。 (它们的使用方法几乎相同。)将其更改为UIAlertController后,将frame属性设置为应用程序主屏幕。 (设置UIAlertView的view.frame并没有帮助)很明显,他们在IOS8中贬低UIAlertViews,虽然它不是很清楚。