问题描述
您好!这是场景.
从导航控制器开始(并且不存在标签栏 - 它从先前的视图控制器推送中隐藏),我初始化一个新的视图控制器并将其推送到导航控制器堆栈上.这个新的 VC 包含一个孤独的 UIView,我以编程方式在其中添加一个具有相同框架的 UIScrollView.(我想避免使用 UIView,但这是我可以将 self.view 分配给某些东西的唯一方法.我怀疑投射 UIScrollView 到 UIView 中的 viewDidLoad 是不可取的.)
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);
}
编译并运行.啊……什么都没有.显然我做错了什么.
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 中的选择性自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!