视图控制器上的强制方向

时间:2017-06-06 13:46:49

标签: ios xcode

在我的应用程序中,我有一个导航控制器,显示强制纵向方向的视图控制器。点击屏幕上的按钮,执行推送并显示视图控制器,并且它没有关于方向的限制,它支持所有方向。问题是,当我点击后退按钮并且我在风景中时,第一个视图控制器以横向显示,而不是纵向显示。 我已经实现了所有方向的方法,例如每个视图控制器的supportedInterfaceOrientation,但是我没有找到强制第一个视图控制器画像的方法。

1 个答案:

答案 0 :(得分:1)

我可以通过在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

另一个代码是修复从另一种类型的景观转换时发生的错误。