我已经为此工作了几个周末以找出正确的解决方案,我尝试了许多其他解决方案,但都无法正常工作。如果您只希望某些 UI 处于横向或纵向,请尝试以下方法。我正在运行 Xcode 7.2.1,我使用 Bool 来设置每个类中的值以及 NSUSerDefaults 我想遵循特定的 UIInterfaceOrientations。每次出现新的 UI 或设备旋转时,都会调用下面的方法,您可以通过在该方法中放置一个 NSLog 检查 bool 的值来检查这一点,并亲自查看它是如何变化的。
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:
在您的 AppDelegate 中执行以下操作
.h @property (assign, nonatomic) BOOL shouldRotate;
.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED{
_shouldRotate = [[NSUserDefaults standardUserDefaults]boolForKey:@"rotateKey"];
NSLog(@"Did I get to InterfaceOrientation \n And the Bool is %d",_shouldRotate);
if (self.shouldRotate == YES)
return UIInterfaceOrientationMaskAllButUpsideDown;
else
return UIInterfaceOrientationMaskPortrait;
}
现在在每个班级中,您都希望有一个特定的 UI 界面来执行此操作
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
// [[AppDelegate sharedAppDel]setShouldRotate:YES];
BOOL rotate = NO;
[[NSUserDefaults standardUserDefaults]setBool:rotate forKey:@"rotateKey"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
-(BOOL)shouldAutorotate
{
return YES;
}
在要旋转的视图中,将 Bool 值更改为 Yes。
现在在你的 AppDelegate 中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
BOOL rotate = NO;
[[NSUserDefaults standardUserDefaults]setBool:rotate forKey:@"rotateKey"];
[[NSUserDefaults standardUserDefaults]synchronize];
return YES;
}
也在你的 AppDelegate 中
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
BOOL rotate = NO;
[[NSUserDefaults standardUserDefaults]setBool:rotate forKey:@"rotateKey"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
希望这对许多开发人员有所帮助。
经过多次测试和测试。
问候
JZ