• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:20:27 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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



Best Answer-推荐答案


Not possible according to Apple Docs.

可能这个词可能需要一个星号。看起来 Apple 并没有设想(或想要)你这样做。但是,根据您的要求,可能会有一种解决方法。

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

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

解决方案

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

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

所以,我的两个 View Controller 都有相同的代码。但是,我在 FirstViewController 中所做的也是覆盖 willAnimateToInterfaceOrientation:duration: 并且基本上 undo 什么是 UIViewController 基础设施确实如此,仅适用于这个仅横向 View Controller 。

FirstViewController.m:

- (void)willAnimateRotationToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation durationNSTimeInterval)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) 我更改了应用的 info plist 文件以将 initial 方向设置为横向(因为我的 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap