ViewController方向更改

时间:2011-11-08 11:03:39

标签: iphone navigation orientation

当iPhone将方向更改为横向时,我正在推动ViewController,而我在更改ViewController的方向时遇到了麻烦。

我使用了那段代码:

    - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
Storage *strg = [Storage sharedStorage];

if ([strg.orient intValue] == 2) 
{
    [[UIApplication sharedApplication] setStatusBarOrientation:
     UIInterfaceOrientationLandscapeRight];

    UIScreen *screen = [UIScreen mainScreen];
    CGFloat screenWidth = screen.bounds.size.width;
    CGFloat screenHeight = screen.bounds.size.height;
    UIView *navView = [[self navigationController] view];
    navView.bounds = CGRectMake(0, 0, screenHeight, screenWidth);
    navView.transform = CGAffineTransformIdentity;
    navView.transform = CGAffineTransformMakeRotation(1.57079633);
    navView.center = CGPointMake(screenWidth/2.0, screenHeight/2.0);
    [UIView commitAnimations];
}
if ([strg.orient intValue] == 1)
{
    [[UIApplication sharedApplication] setStatusBarOrientation:
     UIInterfaceOrientationLandscapeLeft];

    UIScreen *screen = [UIScreen mainScreen];
    CGFloat screenWidth = screen.bounds.size.width;
    CGFloat screenHeight = screen.bounds.size.height;
    UIView *navView = [[self navigationController] view];
    navView.bounds = CGRectMake(0, 0, screenHeight, screenWidth);
    navView.transform = CGAffineTransformIdentity;
    navView.transform = CGAffineTransformMakeRotation(4.71238898);
    navView.center = CGPointMake(screenWidth/2.0, screenHeight/2.0);
    [UIView commitAnimations];
}
}

结果不一致;有时它会进入正确的方向,有时它会颠倒过来。

当我从LandcapeRight前往LandscapeLeft海峡时(反之亦然)它工作正常,问题出现在我进入纵向模式时。

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果您确实在响应设备方向更改,那么您可能不应该使用setStatusBarOrientation。我认为你最好使用shouldAutorotateToInterfaceOrientation和deviceDidRotateSelector通知让你的viewcontrollers旋转到支持的方向。

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceDidRotateSelector:) name: UIDeviceOrientationDidChangeNotification object: nil];

-(void)deviceDidRotateSelector:(NSNotification*) notification {
// respond to rotation here
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//Return YES for supported orientations
return YES;
}