UINavigationController和UITabBarController中的选择性自动旋转 [英] Selective Autorotation within a UINavigationController and UITabBarController

查看:146
本文介绍了UINavigationController和UITabBarController中的选择性自动旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候!这是场景。

从导航控制器开始(并且没有标签栏 - 它在以前的视图控制器推送中隐藏),我初始化一个新的视图控制器和将它推到导航控制器堆栈上。这个新VC包含一个寂寞的 UIView ,我在其中以编程方式添加具有相同帧的UIScrollView。 (我想避免使用 UIView ,但这是我可以将 self.view 分配给某些内容的唯一方法。我怀疑是否会投出 UIScrollView viewDidLoad 中使用strong>到 UIView 。)

Starting with a navigation controller (and no tab bar is present - it is hidden from a previous view controller push), I init a new view controller and push it onto the nav controller stack. This new VC contains a lonesome UIView into which I programmatically add a UIScrollView with the same frame. (I wanted to avoid the UIView, but this was the only way I could get self.view to be assigned to something. I suspect casting a UIScrollView to UIView in viewDidLoad is not advisable.)

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

So now we have a nav bar, and a scroll view. I've set it up to scroll through some images (big surprise, I know!), and that works just fine. Now I want this to support autorotation. So I respond in the VC like so:

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

编译并运行。 Aaaand ......什么都没有。显然我做错了。

Compile and run. Aaaand ... nothing. Obviously I've done something wrong.

现在,我已经阅读了关于 UINavigationController和autorotation ,我怀疑我会以错误的方式解决这个问题,并使其变得比必要的更复杂。

Now, I've already read the post regarding UINavigationController and autorotation, and I get the sneaking suspicion that I'm going about this the wrong way, and making it way more complicated than necessary.

必须有更好的方式来展示支持自动旋转的 UIScrollView 。也许导航控制器正在阻碍,但我不知道如何解决它。

There's got to be a better way to present a UIScrollView that supports autorotation. Perhaps the Nav Controller is getting in the way, but I'm not sure how to get around it.

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

Ideally, I'd like something without any kind of nav bar showing. Instead, we have a toolbar/status bar that appears/hides from the top (like you see when playing video). If the nav bar must remain - or if that's REALLY a shorter-height nav bar I'm seeing when playing video vs. a toolbar, however do I get the thing to rotate around? The thing is, I only want it to rotate in this particular mode, when viewing the pix. Not at any other time.

我敢尝试使用模态VC吗? (Yeccch - 不,那也不对。加上它还有一个导航栏。)

Dare I try using a modal VC? (Yeccch - no, that can't be right either. Plus it has a nav bar anyway.)

推荐答案

你可以解决这没有通过创建UITabBarController类进行子类化。

You can solve this without subclassing by creating a UITabBarController category.

这是处理我的情况的类别实现,其中我有与我的选项卡关联的匿名UINavigationControllers和自定义UIViewController子类作为根视图UINavigationControllers的控制器:

Here is the category implementation which handles my case where I have anonymous UINavigationControllers associated with my tabs and custom UIViewController subclasses as the root view controllers of the UINavigationControllers:

@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。因此,默认行为是启用旋转,我可以从shouldAutorotateToInterfaceOrientation:interfaceOrientation返回NO,仅用于我希望禁用旋转的控制器和方向。

I also have a category on UINavigationController which defaults to returning YES. So, the default behavior is to enable rotation and I can return NO from shouldAutorotateToInterfaceOrientation:interfaceOrientation for just the controllers and orientations for which I wish to disable rotation.

这篇关于UINavigationController和UITabBarController中的选择性自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆