【问题标题】:MBProgressHud is not changing with device orientation.?MBProgressHud 不会随着设备方向而改变。?
【发布时间】:2013-01-17 06:53:36
【问题描述】:

我正在使用 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];
    }
}

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

【问题讨论】:

  • 请注意,如果您将图像 Def​​aultPad.png 重命名为 Default~ipad.png,则可以在第一个代码 sn-p 中省略 if 语句,因为系统会自动识别并在 iPad 上显示它.

标签: iphone objective-c ios device-orientation mbprogresshud


【解决方案1】:

在 MBProgressHUD.m 我改变了

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

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

【讨论】:

    【解决方案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 的源代码中将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,因为它可以很好地处理方向

    【讨论】:

    • 我建议的更改确实适用于 m.. 我认为您没有更改通知类型...在 mbprogresshud 的源代码中...请查看编辑
    • 你可能有 mbprogressHUD 的源代码,所以在我的编辑中显示的 init 方法中执行它
    • 该死...Yarr 使用 svprogresshud.. 无法在 MBProgressHud 上提供更多帮助,因为这些都是为了让它在方向上工作而要做的调整
    • 我认为 svprogresshud 不支持方向。
    • 你去svprogresshud了吗..它确实see
    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多