【问题标题】:Autorotation in iOS 6 & iOS 7iOS 6 和 iOS 7 中的自动旋转
【发布时间】:2014-04-17 15:58:01
【问题描述】:

我想在 iOS 6 和 7 中支持自动旋转。在我的项目中,我有一个带有 4 个 ViewControllers 的 UITabBar。其中只有一个 ViewController 应该支持纵向和横向的自动旋转。其他视图应该只支持 Portrait 样式。

我希望你知道如何实现这个功能。 shouldautorotatetointerfaceorientation 在 iOS 7 中不起作用 :(

我添加了一个 UITabBarConntroller 来控制自动旋转

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    if (self.selectedIndex == 1) 
        return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationPortrait);
    else 
        return UIInterfaceOrientationMaskPortrait;
}

当索引为 1 的视图确实消失时,也许有办法更改方向手册??

希望你有解决办法!

【问题讨论】:

  • 您可以在每个视图控制器中定义这些方法,然后再次将return self.selectedViewController.shouldAutorotate;self.selectedViewController.supportedInterfaceOrientations; 放在Tab Bar Controller 类中。这将从活动视图控制器中检索 shouldAutorotatesupportedInterfaceOrientations 值并将它们用于标签栏控制器。

标签: ios objective-c uiinterfaceorientation


【解决方案1】:

像这样设置方向设置:

在 UITabBarController 中:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

-(NSUInteger)supportedInterfaceOrientations
{
    if (self.selectedViewController) 
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

【讨论】:

  • 这不起作用,仍然可以以不受支持的方向打开视图控制器。
  • 有没有办法在不自动旋转的情况下将一个视图设置为横向,将其他视图设置为纵向?
【解决方案2】:
  • 打开项目

  • 转到“常规”选项卡

  • 在方向选项下选中您需要支持的所有复选框方向

转到第一个视图控制器的实现并添加以下代码:

- (BOOL)shouldAutorotate
{
    return YES;
}

也支持iOS 6的实现方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationMaskAll);
}

转到代表您的选项卡的所有其他视图控制器并设置:

- (BOOL)shouldAutorotate
{
    return NO;
}

也支持iOS 6的实现方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

也检查这个link

【讨论】:

  • 我已经添加了链接检查它
  • 是的,旋转不会“起作用”,但仍然可以在“错误”(即不受支持的)界面方向切换到视图控制器。
最近更新 更多