【问题标题】:how can i do landscape disable for just one viewController iOS我怎样才能为一个viewController iOS做横向禁用
【发布时间】:2015-01-05 23:15:44
【问题描述】:

我的项目有 UITabbar、UınavigationBar 和 UIViewContoller。

-UITabbar
 —UINavigationController
   —UIViewController

问题:我怎样才能横向禁用一个视图控制器?我只想纵向视图,但只有一个视图。

【问题讨论】:

标签: ios orientation


【解决方案1】:

disable autorotate on a single UIViewController in iOS6

将此添加到您的应用委托

- (NSUInteger) application:(UIApplication *)application     supportedInterfaceOrientationsForWindow:(UIWindow *)window {

if ([[window.rootViewController presentedViewController] isKindOfClass:[YourViewController class]])
    return UIInterfaceOrientationMaskPortrait;
else
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

编辑 2

我最近发现了这一点,它对我来说非常有效。将此代码添加到您的视图控制器中

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

并在您的viewWillAppear 添加

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    ///Disable orientation changes
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}

【讨论】:

  • 部分工作。如果我从 Landscape ViewController 移回 Portrait ViewController。它没有给出预期的结果。如果您对此有任何解决方案,请提供反馈
  • @LeoMoon85 答案已更新。让我知道它是否对您有帮助。
  • 我还添加了一件事...在 viewDidAppear, [UIView setAnimationsEnabled:NO];再次 [UIView setAnimationsEnabled:YES];在视图中会出现。当从横向移回纵向时,它有助于更​​好地查看,反之亦然。谢谢。
猜你喜欢
  • 2019-09-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 2021-10-25
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 2014-01-03
相关资源
最近更新 更多