2011-11-08 86 views
1

当iPhone将方向更改为横向时,我正在推送ViewController,并且无法更改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 strait(反之亦然)时,它工作正常,问题只在我进入肖像模式时出现。

任何想法我做错了什么?

回答

1

如果你真的在响应设备方向改变,他们可能不应该使用setStatusBarOrientation。我认为你最好让viewcontroller使用shouldAutorotateToInterfaceOrientation和deviceDidRotateSelector通知旋转到支持的方向。

[[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; 
} 
+0

我试过了。将无法工作。不知道为什么,我发现很多其他人在组合方向更改和导航时遇到了这个问题。 – urilevy2

+0

嗯,这对我有用。我倾向于唯一的问题是确保我回应UIDeviceOrientationUnknown以及其他方向。但是,我不使用笔尖。我编码的一切,这可能是为什么我没有问题。我仍然认为你应该避免使用setStatusBarOrientation,因为这实际上并没有改变设备方向 - 如果你询问设备它的设备方向是什么,即使你已经设置了状态栏方向为别的东西。 – ader

+0

我不知道我错过了什么,当我尝试之前,但现在它的作品!谢谢!! – urilevy2