問題描述
您好!這是場景.
從導航控制器開始(并且不存在標簽欄 - 它從先前的視圖控制器推送中隱藏),我初始化一個新的視圖控制器并將其推送到導航控制器堆棧上.這個新的 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 中的選擇性自動旋轉的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!