我正在使用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];
}
}
如何让装载指示器随设备方向旋转?
答案 0 :(得分:3)
在MBProgressHUD.m中我改变了
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
到
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
已收到方向通知,但statusBar尚未轮换。
答案 1 :(得分: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,因为它很好地处理方向