在我的应用程序中,我有一个导航 Controller ,它显示 View Controller 强制为纵向。点击屏幕上的按钮会执行推送并显示 View Controller ,并且它没有方向限制,它支持所有方向。问题是当我点击后退按钮并且我处于横向时,第一个 View Controller 显示为横向,而不是预期的纵向。 我已经实现了所有方向方法,例如每个 View Controller 的 supportedInterfaceOrientation,但我没有找到强制第一个 View Controller 纵向的方法。
最佳答案
我能够通过在 viewWillAppear
事件中调用以下内容来强制特定 View 的方向:
// LOCK SCREEN ORIENTATION TO LANDSCAPE
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate setLockLandscape:YES];
// CHECK CURRNET ORIENTATION
if([[UIDevice currentDevice] orientation] != UIInterfaceOrientationLandscapeRight){
// TRANSITION ORIENTATION TO LANDSCAPE
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}else{
// SET ORIENTATION TO PORTRAIT
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
// TRANSITION ORIENTATION TO LANDSCAPE
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
转换 View
这会将 View 设置为特定方向的动画,但方向不一定会被锁定:
// TRANSITION ORIENTATION TO LANDSCAPE
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
锁定方向
这将锁定 View :
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate setLockLandscape:YES];
我能够从我在 appDelegates
中创建的属性 lockLandscape
调用 [appDelegate setLockLandscape:YES];
以及以下内容功能:
@property(分配,非原子)BOOL lockLandscape;
//////////////////////////////////////////
// DELAGATE FUNCTION - LOCK ORIENTATION
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
// CHECK LOCK LANDSCAPE
if (self.lockLandscape){
// LOCK LANDSCAPE
return UIInterfaceOrientationMaskLandscape;
}else{
// LOCK PORTRAIT
return UIInterfaceOrientationMaskPortrait;
}
} // END - FUNCTION DELAGATE
另一个代码是修复从我强制的另一种景观类型转换时发生的错误。
关于ios - 在 View Controller 上强制方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44391814/