iphone - UINavigationController 和 UITabBarController 中的选择性自动旋转

标签 iphone uinavigationcontroller uiscrollview uitabbarcontroller rotation

您好!这是场景。

从导航 Controller (并且不存在选项卡栏 - 它在之前的 View Controller 推送中隐藏)开始,我初始化一个新的 View Controller 并将其推送到导航 Controller 堆栈上。这个新的 VC 包含一个孤独的 UIView,我以编程方式向其中添加一个具有相同框架的 UIScrollView。 (我想避免 UIView,但这是我可以将 self.view 分配给某些东西的唯一方法。我怀疑转换 UIScrollViewviewDidLoad 中使用 UIView。)

现在我们有了一个导航栏和一个 ScrollView 。我已经将其设置为滚动浏览一些图像(我知道,这是很大的惊喜!),效果很好。现在我希望它支持自动旋转。所以我在 VC 中回复如下:

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

编译并运行。啊啊……没什么。显然我做错了什么。

现在,我已经阅读了有关 UINavigationController and autorotation 的帖子,我隐隐怀疑我的做法是错误的,并且让事情变得比必要的更加复杂。

必须有一种更好的方法来呈现支持自动旋转的UIScrollView。也许导航 Controller 妨碍了,但我不知道如何解决它。

理想情况下,我想要不显示任何导航栏的东西。相反,我们有一个从顶部显示/隐藏的工具栏/状态栏(就像您在播放视频时看到的那样)。如果导航栏必须保留 - 或者如果我在播放视频时看到的导航栏与工具栏相比确实是一个高度较短的导航栏,但是我能让它旋转吗?问题是,我只希望它在查看像素时以这种特定模式旋转。其他时间都不会。

我敢尝试使用模态 VC 吗? (是的 - 不,这也不对。而且它还有一个导航栏。)

最佳答案

您可以通过创建 UITabBarController 类别来解决此问题,而无需子类化。

这是处理我的情况的类别实现,其中我有与我的选项卡关联的匿名 UINavigationController 和作为 UINavigationController 的 Root View Controller 的自定义 UIViewController 子类:

@implementation UITabBarController (Rotation)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{   
    if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
        UIViewController *rootController = [((UINavigationController *)self.selectedViewController).viewControllers objectAtIndex:0];
        return [rootController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

@end

我在 UINavigationController 上还有一个类别,默认返回 YES。因此,默认行为是启用旋转,并且对于我希望禁用旋转的 Controller 和方向,我可以从 shouldAutorotateToInterfaceOrientation:interfaceOrientation 返回 NO。

关于iphone - UINavigationController 和 UITabBarController 中的选择性自动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1196758/

相关文章:

ios - View 边界外的 subview 上的手势识别器

iphone - 如何将消息从一个对象发送到另一个对象?

ios - UIToolBar 结合 pushViewController

swift - 页面控件中的多个 UILabel

ios - 为什么滚动 UITableView 比滚动 UIScrollView 响应更快?

iphone - XCode 中的 header 搜索路径 - 是否可以设置宏或变量,以便它们与 SDK 版本相关?

iphone - 关于iphone中的Paypal支付? (说明如下)

iphone - 不使用 NavigationController 的 UIViewController 的 Push-Pop 类动画

iOS 13 UIBarButtonItem 使用 UISearchController 时不可点击且重叠 UINavigationBars

ios - 当 scrollView 设置 pagingEnabled 为 YES 时,点击 ScrollView 调用 `scrollViewWillBeginDecelerating`