9

I'm wondering if anyone knows a good solution to the fact that UIAlertViews and UIAlertControllers won't scroll on iOS 8? Here is an example:

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

Running that code on iOS 7 and 8 produces the following results. (Changing it to UIAlertController makes no difference).

iOS 8:
iOS 8:

iOS 7:
iOS 7:

As you can see it clearly scrolls on iOS 7 but not on iOS 8. Is there some property that I'm missing here or is it just a beta bug?

bummi
  • 27,123
  • 14
  • 62
  • 101
Rick
  • 3,240
  • 2
  • 29
  • 53
  • It maybe a new implementation ... or a bug. Alert views aren't really supposed to be used to display LONG strings ... May be Apple trying to reinforce adherence to the HIG? – CW0007007 Jul 10 '14 at 08:38
  • If it was really the case, how come they have been providing scrolling since it was first introduced? Also, there is no good alternative, the only option is presenting a view controller modally and that's overdoing it in this case. – Rick Jul 10 '14 at 08:45
  • Well they can change it as and when they want ... Tidying up the user experience ? I don't know. May just be a bug like you said... Write your own 'alert view' ? Wouldn't be difficult. Sure there are third party ones too if you don't have time. – CW0007007 Jul 10 '14 at 08:47
  • I've only done a bit in iOS 8. Waiting until Beta 4 for this exact reason ! – CW0007007 Jul 10 '14 at 08:47
  • 1
    Yes, I guess it's a "wait and see" case. It would be ridiculous if they removed the only useful way of showing information on the screen without completely covering the UI. And also, regarding the HIG, as you can see the buttons disappear if the text is too long (Also a HIG violation but that's not my point) leading to the alert view being stuck and forcing a restart of the app. Not really a good user experience ;) – Rick Jul 10 '14 at 08:49
  • ha yeah. Seems like a bug then, the button disappearing may be related to non scrolling.. but then again they've truncated the text so looks intentional ... hmmmm wait and see like you said; or take a look at the API and see if there's a enableScrolling property ? – CW0007007 Jul 10 '14 at 08:52
  • in Xcode6-Beta5 this issue is partially solved. The button are shown. But scrolling still does not work unless you rotate your device once to landscape. – muzz Aug 10 '14 at 12:06
  • That's neat. Maybe it was a bug then. Let's cross our fingers for Beta 6. – Rick Aug 15 '14 at 09:14
  • Any update on this issue? – AlexanderN Sep 01 '14 at 15:10
  • Last I tested it was still broken but that was probably in the previous beta, I actually haven't tried it recently. It's not a super critical function in our app so it got pushed down the list in favor of other items. – Rick Sep 01 '14 at 19:57
  • This is still occurring.. At least the title is showing now, I guess – Max Chuquimia Sep 26 '14 at 02:01
  • Yeah, it's showing and the button works so you can actually dismiss it. That's at least good. – Rick Sep 26 '14 at 05:43
  • Still happens on 8.0.2, but I think text size is smaller now – jcesarmobile Sep 29 '14 at 07:02

5 Answers5

11

Although I dunno the right way, I could solve the issue anyway. Seems like the view scrolls if you set the frame property (but not CGRectZero). It worked fine on 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
  • 745
  • 1
  • 10
  • 25
  • Sorry, you were right, setting the view frame to the size of the screen works fine. Maybe you should change your code to use a long message as example – jcesarmobile Sep 30 '14 at 07:52
  • I mean a long text for the message field instead of message:@"Hello." – jcesarmobile Sep 30 '14 at 08:55
  • This might do it, I'll give it a try next time I'm at work. Will force some duplicated code however since it doesn't work on iOS 7 but it's better than nothing. – Rick Sep 30 '14 at 09:17
  • 1
    While I haven't tried the code myself (I ended up switching jobs and this got forgotten) I'll take your word for it working and mark this as the answer. – Rick Oct 26 '14 at 12:40
  • 1
    This works in portrait mode but not in landscape for me – Mohamed Hafez Dec 20 '14 at 06:28
  • 1
    this fix crash on iOS 8.3 if you add a textfield, but they have fixed the problem on iOS 8.3, so you should add this line to avoid the crash and fix the problem on previous versions if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.3) { alertController.view.frame = [[UIScreen mainScreen] applicationFrame]; } – jcesarmobile Apr 11 '15 at 11:21
  • Application frame is deprecated since iOS 9. Use `alertController.view.frame = [[UIScreen mainScreen] bounds];` instead – NSNoob Oct 07 '16 at 12:11
3

cafedeichi's solution didn't work for me, so I ended up using DTAlertView which works both on iOS 8 and iOS 7.

kernix
  • 7,970
  • 3
  • 28
  • 44
3

The problem has been fixed on iOS 8.3 I've been using cafedeichi solution, but if isn't working on landscape mode for iPhones and iPod touch devices, and if you add a textfield it will crash on iOS 8.3, so I'm using this code right now that fixes the two problems mentioned:

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{};
jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
  • 1
    This is some good piece of code! Should be the accepted answer. Would be even more splendid with a reference to the actual bug fix in 8.3. – ullstrm Jun 01 '15 at 14:34
  • I have not seen an official bug fix reference, just tested and it was working fine without this code – jcesarmobile Jun 01 '15 at 15:09
0

Use this :

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] ;
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
0

Any UIAlertView instances have to first be changed to UIAlertController instances. (They can be used pretty much the same way.) After changing it to a UIAlertController, set the frame property to the application main screen. (setting view.frame of UIAlertView doesn't help) Evidently, they are depreciating UIAlertViews in IOS8, although it isn't as clear as it should be.

Vette
  • 511
  • 5
  • 10