ios - 在 View Controller 上强制方向

标签 ios xcode

在我的应用程序中,我有一个导航 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/

相关文章:

ios - iOS/Objective-C:核心数据提取中的NSPredicate和NULL值

ios - 检查是否在特定日期添加了 Realm 数据

xcode - FirebaseApp.configure()使用 Unresolved 标识符 'FirebaseApp'错误

ios - Xamarin.Forms 方法在某些设备上执行两次

objective-c - Mac 应用程序的一般设计,基于文档还是?

ios - 无法提交 - 弄乱了我的源代码控制,如何解决?

iphone - rss 阅读 iphone/ipad 应用程序

ios - 无法在 swift 中调用非函数类型 'CLLocationCordinate2d' 的值

ios - ios获取当前应用程序图标的方法

ios - 检查当前时间是否在午夜后考虑的时间范围内 - Swift