0

我正在使用 MBProgressHud 在初始视图上显示加载指示器,但这不会随设备方向而改变。我的代码是:

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

我已经将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];
    }
}

至:

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

如何让加载指示器随设备方向旋转?

4

2 回答 2

3

在 MBProgressHUD.m 我改变了

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

已收到方向通知,但状态栏尚未旋转。

于 2013-01-16T22:53:36.810 回答
2

尝试这个:-

- (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);
}

如果它不起作用..您可以在 MBProgressHUD 的源代码中更改 with:UIDeviceOrientationDidChangeNotification-UIApplicationDidChangeStatusBarOrientationNotification

- (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;
}

在上面的代码中,用 UIApplicationDidChangeStatusBarOrientationNotification 更改 UIDeviceOrientationDidChangeNotification。

这只是一种解决方法,因为 MBProgressHud 始终存在轮换问题。

我猜 MBProgressHud 有很多问题,你应该改用svprogresshud因为它可以很好地处理方向

于 2012-05-16T07:12:28.647 回答