【发布时间】:2017-07-11 07:50:43
【问题描述】:
我遇到了麻烦,如果我将手机倒置,我的应用就会倒置旋转。
我有一个包含两个场景的故事板文件: NavigationControllerScenes and MainScene / SettingsScene
在 info.plist 中,我选中了以纵向或倒置方向开始的框。
在我添加的应用委托中:
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
#else
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
#endif
{
return UIInterfaceOrientationMaskPortrait;
}
在设置视图控制器中:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
{
return UIInterfaceOrientationPortraitUpsideDown;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (BOOL)shouldAutorotate // iOS 6 autorotation fix
{
return YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations{
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
}
在主视图控制器中
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
{
return UIInterfaceOrientationPortraitUpsideDown;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (BOOL)shouldAutorotate // iOS 6 autorotation fix
{
return YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
可以旋转到横向和纵向,但不能颠倒纵向。 如果我取消选中 info.plist 中的纵向框并仅选中倒置框,则应用程序将在开始时崩溃。 所以我认为缺少一个设置...
我没有得到的是,我可以在只有一个场景的不同应用程序中成功使用这些代码 sn-ps - 所以我认为错误来自多个场景。 也许我需要创建一个导航控制器来覆盖一些方法。
还有一点: 这不起作用 - 应用程序执行语句但没有任何反应:
NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
在具有一个场景的应用程序中有效!
【问题讨论】:
-
我希望这个链接对你有用stackoverflow.com/questions/12640870/…
标签: ios objective-c orientation landscape-portrait portrait