簡體   English   中英

MBProgressHud不會隨設備方向改變。

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

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

如何使加載指示器隨着設備方向旋轉?

在MBProgressHUD.m中,我更改了

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

方向通知已收到,但statusBar尚未旋轉。

嘗試這個:-

- (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的源代碼中的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;
}

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

這只是一個變通方法,因為MBProgressHud始終存在旋轉問題。

我猜MBProgressHud給了很多問題,您應該改用svprogresshud,因為它可以很好地處理方向

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM