[英]Force orientation on view controller
在我的应用程序中,我有一个导航控制器,该导航控制器显示了强制以纵向显示的视图控制器。 轻按屏幕上的按钮即可执行推入操作,并显示一个视图控制器,它没有方向限制,它支持所有方向。 问题是,当我点击后退按钮并且我处于横向时,第一个视图控制器以横向显示,而不是纵向显示。 我已经实现了所有定向方法,例如为每个视图控制器提供了supportedInterfaceOrientation,但是我没有找到一种方法来强制第一个视图控制器变为纵向。
我可以通过在viewWillAppear
事件中调用以下命令来强制特定视图的定向:
// 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"];
}
这会将视图动画化为特定的方向,但是该方向不一定会被锁定:
// TRANSITION ORIENTATION TO LANDSCAPE
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
这将锁定视图:
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate setLockLandscape:YES];
我可以打电话给[appDelegate setLockLandscape:YES];
从属性lockLandscape
我在创建appDelegates
以及以下功能:
@property (assign, nonatomic) 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
另一个代码是修复从我不强制使用的另一种景观过渡时发生的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.