iphone - 仅允许将 UITabViewController 中的一个选项卡的方向更改为横向

标签 iphone ios objective-c device-orientation uitabview

这可能吗?我怎样才能做到这一点?

最佳答案

Not possible according to Apple Docs.

“可能”一词可能需要加一个星号。看起来苹果公司显然没有想到(或不希望)你这样做。但是,根据您的要求,可能有解决方法。

免责声明:这是一种黑客行为。我并不是说这是一个很好的 UI,只是想向 Eli 展示什么是可能的。

我构建了一个示例,从用于构建选项卡式应用程序的 Xcode 模板开始。它有两个 View Controller :FirstViewController 和SecondViewController。我决定将 FirstViewController 设为仅横向 View 。在 Interface Builder(Xcode UI 设计模式)中,我将 FirstViewController View 的方向设置为横向,并将其尺寸设置为 480 宽 x 251 高(这里假设是 iPhone/iPod)。

解决方案

现在,似乎有必要让所有标签栏的 View Controller 声明支持纵向和横向自动旋转。例如:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

所以,我的两个 View Controller 都有相同的代码。但是,我在 FirstViewController 中所做的也是重写 willAnimateToInterfaceOrientation:duration: ,并且本质上是撤消 UIViewController 基础设施的内容确实如此,仅适用于这个仅横向的 View Controller 。

FirstViewController.m:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
    [super willAnimateRotationToInterfaceOrientation:interfaceOrientation duration:duration];

    CGAffineTransform viewRotation;
    CGRect frame;

    if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
        viewRotation = CGAffineTransformIdentity;
        // TODO: change to dynamically account for status bar and tab bar height
        frame = CGRectMake(0, 0, 480, 320 - 20 - 49);        
    } else {
        viewRotation = CGAffineTransformMakeRotation(M_PI_2);
        // TODO: change to dynamically account for status bar and tab bar height
        frame = CGRectMake(0, 0, 320, 480 - 20 - 49);        
    }

    // undo the rotation that UIViewController wants to do, for this view heirarchy
    [UIView beginAnimations:@"unrotation" context: NULL];
    [UIView setAnimationDuration: duration];

    self.view.transform = viewRotation;
    self.view.frame = frame;

    [UIView commitAnimations];
}

这样做的结果是,标签栏将始终随设备旋转。这可能是一个要求,让你的双方向 View (例如 SecondViewController)进行自动旋转。但是,FirstViewController 的实际 View 内容现在不会旋转。无论用户如何转动设备,它都保持横向。那么,也许这对您来说已经足够了?

还值得注意:

1) 我更改了应用程序的信息 plist 文件,将初始方向设置为横向(因为我的 FirstViewController 是横向方向):

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>        
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIInterfaceOrientation</key>
<string>UIInterfaceOrientationLandscapeRight</string>

2) 在 FirstViewController.xib 中,我将主/父 UIView 设置为不自动调整 subview 大小。根据您的 View 层次结构,您可能也希望在其他 subview 中更改此属性。您可以尝试该设置。

现在,随着状态栏和选项卡栏的旋转,横向 View 的可用尺寸确实发生了一些变化。因此,您可能需要稍微调整一下布局。但是,基本上,无论用户如何握住设备,您仍然会获得用于显示横向内容的 View 。

结果

Watch Youtube demo of running app

关于iphone - 仅允许将 UITabViewController 中的一个选项卡的方向更改为横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10889728/

相关文章:

ios - xcode 6.1 启动屏幕启动图像空白

ios - 将另一个类的项目添加到另一个类的属性。 Objective-C

iphone - 是否可以替换已发布的 iPhone 应用程序中的屏幕截图?

iphone - 我如何在 xcode 4 中引用一个单独的项目?

android - 需要在 iOS 中安装第二个应用程序

iphone - 将 3d 对象中心坐标转换为 2d 可见视口(viewport)坐标

ios - SkSpriteNode在通用游戏中的位置

ios - 推送通知限制

objective-c - 如何发出 Yelp API 请求?

ios - 将 UIImage 的 "Tick Marks"添加到 UISlider