0

I am using MBProgressHud to show a loading indicator on a splash view but this does not change with device orientation. My code is:

splashView = [[UIImageView alloc] initWithFrame:self.window.frame];

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
 {
  splashView.image = [UIImage imageNamed:@"DefaultPad.png"];  
 }
 else
 {
    splashView.image = [UIImage imageNamed:@"Default.png"];  
 }

  hud = [[MBProgressHUD alloc]initWithView:splashView];
  [splashView addSubview:hud];

  hud.userInteractionEnabled=NO;
  hud.labelText = @"Loading...";

  [hud show:YES];

 [self.window addSubview:splashView];
 [self performSelector:@selector(Load_FirstView) withObject:nil afterDelay:3];
 [self.window makeKeyAndVisible];

and I have changed the line in MBProgressHud.m file from

- (void)deviceOrientationDidChange:(NSNotification *)notification { 

 NSLog(@"in device orientation ");
    UIView *superview = self.superview;
    if (!superview) {
        return;
    } else if ([superview isKindOfClass:[UIWindow class]]) {  // here changes have done
        [self setTransformForCurrentOrientation:YES];
    } else {
        self.bounds = self.superview.bounds;
        [self setNeedsDisplay];
    }
}

to:

- (void)deviceOrientationDidChange:(NSNotification *)notification { 

 NSLog(@"in device orientation ");
    UIView *superview = self.superview;
    if (!superview) {
        return;
    } else if ([superview isKindOfClass:[UIImageView class]]) {
        [self setTransformForCurrentOrientation:YES];
    } else {
        self.bounds = self.superview.bounds;
        [self setNeedsDisplay];
    }
}

How can I get the loading indicator to rotate with device orientation?

Chris
  • 44,602
  • 16
  • 137
  • 156
  • Note that if you rename your image DefaultPad.png to Default~ipad.png, you can leave out your if statement in your first code snippet, as the system automatically recognizes and displays it on iPad. – Scott Berrevoets Jun 11 '12 at 06:31

2 Answers2

3

In MBProgressHUD.m I changed

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

to

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

The orientation notification had been received, but the statusBar had not rotated yet.

mick80234
  • 897
  • 8
  • 9
2

Try This:-

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
       //code for portrait
  [hud release];
  hud = [[MBProgressHUD alloc]initWithView:splashView];
    }       
    else 
    { 
       //code for Landscape 
  [hud release];
  hud = [[MBProgressHUD alloc]initWithView:splashView];
    }
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
            interfaceOrientation == UIInterfaceOrientationPortrait || 
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

if it does not work.. you can change the UIDeviceOrientationDidChangeNotification with UIApplicationDidChangeStatusBarOrientationNotificationin the source code of MBProgressHUD:-

- (id)initWithView:(UIView *)view {
        // Let's check if the view is nil (this is a common error when using the windw initializer above)
        if (!view) {
                [NSException raise:@"MBProgressHUDViewIsNillException" 
                                        format:@"The view used in the MBProgressHUD initializer is nil."];
        }
        id me = [self initWithFrame:view.bounds];
        // We need to take care of rotation ourselfs if we're adding the HUD to a window
        if ([view isKindOfClass:[UIWindow class]]) {
                [self setTransformForCurrentOrientation:NO];
        }
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) 
                                                                                                 name:UIDeviceOrientationDidChangeNotification object:nil];

        return me;
}

In the above code change UIDeviceOrientationDidChangeNotificationwith UIApplicationDidChangeStatusBarOrientationNotification.

It is just a work-around as rotation issue was always there with MBProgressHud .

I Guess MBProgressHud is giving a lo of problems , you should instead switch to svprogresshud as it handles orientations well

Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
  • well the changes i suggeted did work for m.. i think u did not change the notification type...in the source code of the mbprogresshud ...please see the edits – Abhishek Singh May 16 '12 at 07:44
  • you might have the souce code with you of mbprogressHUD so do it there in the init method i showed in my edits – Abhishek Singh May 16 '12 at 07:51
  • Damn...Yarr use svprogresshud..can't help more on the MBProgressHud as these are tweaks to do to get it working in orientation – Abhishek Singh May 16 '12 at 08:03
  • I think svprogresshud doesn't support orientation. –  May 16 '12 at 08:33
  • did you go to svprogresshud ..it does[see](https://github.com/samvermette/SVProgressHUD#readme) – Abhishek Singh May 16 '12 at 09:02
  • yeah firstly I have used it and it was not supporting device orientation thats why I used MBProgresshud –  May 16 '12 at 09:08
  • I added what you suggested and It works fine. Thanks. +1 vote I changed as following, [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; – Vijay Aug 28 '13 at 05:03