我有麻烦,迫使我的应用程序旋转倒转,如果我是拿着手机倒转。
我有一个有两个场景的故事板文件:NavigationControllerScenes和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中的肖像框,只选中倒挂框,该应用程序将在一开始崩溃。所以我觉得没有设置..。
我不明白的是,我可以在一个不同的应用程序中成功地使用这些代码片段,只有一个场景--所以我认为错误来自多个场景。也许我需要创建一个覆盖某些方法的navigationController。
还有一件事:这是行不通的--应用程序执行语句,但什么也没有发生:
NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];在这个只有一个场景的应用程序中,它是有效的!
发布于 2017-07-25 08:12:53
最后,我自己找到了答案:
创建一个CustomNavigationController类如下所示:
// .h file
#import <UIKit/UIKit.h>
@interface CustomNavigationController : UINavigationController
@end
// .m file
#import "CustomNavigationController.h"
@interface CustomNavigationController ()
@end
@implementation CustomNavigationController
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
@end在故事板中,单击导航控制器场景。在自定义类部分如图所示中添加创建的类
https://stackoverflow.com/questions/45028500
复制相似问题