【问题标题】:UIAlertController/UIAlertView scrolling on iOS 8UIAlertController/UIAlertView 在 iOS 8 上滚动
【发布时间】:2014-07-10 08:32:39
【问题描述】:

我想知道是否有人知道 UIAlertViews 和 UIAlertControllers 不会在 iOS 8 上滚动这一事实的良好解决方案?这是一个例子:

[[[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 上却没有。我在这里缺少某些属性还是只是一个 beta 错误?

【问题讨论】:

  • 它可能是一个新的实现......或者一个错误。警报视图实际上不应该用于显示长字符串......可能是苹果试图加强对 HIG 的遵守?
  • 如果真的是这样,为什么他们从第一次引入滚动就一直提供滚动功能?此外,也没有好的选择,唯一的选择是以模态方式呈现视图控制器,在这种情况下,这太过分了。
  • 好吧,他们可以根据需要随时更改它...整理用户体验?我不知道。可能只是像您说的那样是一个错误...编写您自己的“警报视图”?不会很困难。如果你没有时间,当然也有第三方的。
  • 我在 iOS 8 中只做了一点。正是因为这个原因,等到 Beta 4!
  • 是的,我猜这是一个“观望”案例。如果他们删除了在屏幕上显示信息的唯一有用方式而不完全覆盖 UI,那将是荒谬的。此外,关于 HIG,您可以看到如果文本太长,按钮会消失(也是 HIG 违规,但这不是我的意思)导致警报视图卡住并强制重新启动应用程序。不是很好的用户体验;)

标签: ios objective-c


【解决方案1】:

虽然我不知道正确的方法,但无论如何我都可以解决问题。 如果您设置 frame 属性(但不是 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{};

【讨论】:

  • 抱歉,您是对的,将视图框架设置为屏幕大小可以正常工作。也许您应该更改代码以使用长消息作为示例
  • 我的意思是消息字段的长文本,而不是 message:@"Hello."
  • 虽然我自己没有尝试过代码(我最终换了工作,这被遗忘了)我会相信你的话,并将其标记为答案。
  • 这适用于纵向模式,但不适用于我的横向模式
  • 如果您添加文本字段,此修复程序会在 iOS 8.3 上崩溃,但他们已经修复了 iOS 8.3 上的问题,因此您应该添加此行以避免崩溃并修复以前版本的问题 if ([ [[UIDevice currentDevice] systemVersion] floatValue]
【解决方案2】:

cafedeichi 的解决方案对我不起作用,所以我最终使用了DTAlertView,它适用于 iOS 8 和 iOS 7。

【讨论】:

    【解决方案3】:

    该问题已在 iOS 8.3 上得到修复 我一直在使用 cafedeichi 解决方案,但如果不适用于 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{};
    

    【讨论】:

    • 这是一段很好的代码!应该是公认的答案。参考 8.3 中的实际错误修复会更加出色。
    • 我还没有看到官方的错误修复参考,只是经过测试,没有这段代码也可以正常工作
    【解决方案4】:

    使用这个:

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

    【讨论】:

      【解决方案5】:

      任何 UIAlertView 实例都必须首先更改为 UIAlertController 实例。 (它们的使用方式几乎相同。)将其更改为 UIAlertController 后,将 frame 属性设置为应用程序主屏幕。 (设置 UIAlertView 的 view.frame 没有帮助)显然,他们正在贬低 IOS8 中的 UIAlertViews,尽管它并不像应有的那样清晰。

      【讨论】:

        猜你喜欢
        • 2014-11-22
        • 1970-01-01
        • 2014-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多