简体   繁体   English

MBProgressHud不会随设备方向改变。

[英]MBProgressHud is not changing with device orientation.?

I am using MBProgressHud to show a loading indicator on a splash view but this does not change with device orientation. 我正在使用MBProgressHud在初始视图上显示加载指示器,但这不会随着设备方向而改变。 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 并且我已经从MBProgressHud.m文件中更改了行

- (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? 如何使加载指示器随着设备方向旋转?

In MBProgressHUD.m I changed 在MBProgressHUD.m中,我更改了

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

to

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

The orientation notification had been received, but the statusBar had not rotated yet. 方向通知已收到,但statusBar尚未旋转。

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 UIApplicationDidChangeStatusBarOrientationNotification in the source code of MBProgressHUD:- 如果不起作用,则可以使用MBProgressHUD的源代码中的UIApplicationDidChangeStatusBarOrientationNotification更改UIDeviceOrientationDidChangeNotification

- (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. 在上面的代码中,使用UIApplicationDidChangeStatusBarOrientationNotification更改UIDeviceOrientationDidChangeNotification。

It is just a work-around as rotation issue was always there with MBProgressHud . 这只是一个变通方法,因为MBProgressHud始终存在旋转问题。

I Guess MBProgressHud is giving a lo of problems , you should instead switch to svprogresshud as it handles orientations well 我猜MBProgressHud给了很多问题,您应该改用svprogresshud,因为它可以很好地处理方向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM