编程旋转iOS应用倒挂使用Objective-C的

问题描述:

我想用在视图控制器下面的代码转动我的应用程序颠倒画像:编程旋转iOS应用倒挂使用Objective-C的

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


- (UIInterfaceOrientationMask) supportedInterfaceOrientations 

{ 
    return [super supportedInterfaceOrientations] | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait; 
} 

在AppDelegate中:

- (UIInterfaceOrientationMask) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

{ 
    return UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait; 
} 

当用户按下按钮

NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown]; 
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 

该应用程序不会旋转,我不知道为什么。 有人可以提供一个提示? 谢谢 我在info.plist中添加了方向,并在部署信息部分的常规设置中设置了复选框。

+0

您的应用程序是否有导航控制器? –

+0

是的,它有。为什么?。 – Johan

我是否正确理解您,您希望以编程方式旋转用户界面,独立于设备的物理方向?

这不是它的工作方式:当检测到设备(物理)旋转时,iOS调用委托方法(shouldAutorotate ...等)。用户将其颠倒。如果需要,在代表方法中,您有机会将视图应用于新方向。

在模拟器中,您可以通过Alt + Left/Alt + Right键模拟设备旋转。

+0

嗨,不,实际上我希望用户旋转设备本身,应用程序也应该旋转 - 否则一切都会颠倒,对吧? – Johan

您不能以编程方式旋转方向,这取决于设备的传感器。但是,您可以旋转视图的图层。

self.view.layer.transform = CGAffineTransformMakeRotation(M_PI_2); 
+0

这非常奇怪,因为方向更改在应用程序中工作,我只有一个视图 - 我不知道问题是否与具有多个视图控制器 – Johan