Iphone ViewController方向更改

Iphone ViewController方向更改,iphone,navigation,orientation,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 int

当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转到LandCapeLeft strait时(反之亦然),效果很好,问题只是当我转到肖像模式时


你知道我做错了什么吗?

如果你真的对设备方向的改变做出反应,你可能不应该使用SetStatusBaroOrientation。我认为您最好使用shouldAutorotateToInterfaceOrientation和DeviceIDRotateTelector通知将ViewController旋转到支持的方向

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

如果您真的对设备方向的更改做出响应,那么您可能不应该使用SetStatusBaroOrientation。我认为您最好使用shouldAutorotateToInterfaceOrientation和DeviceIDRotateTelector通知将ViewController旋转到支持的方向

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

我已经试过了。不起作用。不知道为什么,我发现很多人在结合方向改变和导航时都有这个问题。嗯,这对我来说很有效。我唯一的问题是确保我响应UIDeviceOrientationUnknown以及其他方向。但是,我不使用笔尖。我把所有东西都编码了,这可能就是我没有问题的原因。我仍然认为你应该避免使用SetStatusBaroOrientation,因为这实际上不会改变设备的方向-如果你问设备它的设备方向是什么,它仍然会说它的真实设备方向是什么,即使你已经将状态栏方向设置为其他方向。我不知道我以前尝试过这一点时错过了什么,但是现在它工作了!谢谢我已经试过了。不起作用。不知道为什么,我发现很多人在结合方向改变和导航时都有这个问题。嗯,这对我来说很有效。我唯一的问题是确保我响应UIDeviceOrientationUnknown以及其他方向。但是,我不使用笔尖。我把所有东西都编码了,这可能就是我没有问题的原因。我仍然认为你应该避免使用SetStatusBaroOrientation,因为这实际上不会改变设备的方向-如果你问设备它的设备方向是什么,它仍然会说它的真实设备方向是什么,即使你已经将状态栏方向设置为其他方向。我不知道我以前尝试过这一点时错过了什么,但是现在它工作了!谢谢