2012-07-06 62 views
3

所以,我放棄了試圖解決我一直有在我的控制器將在不打電話給我shouldAutorotateToInterfaceOrientation:方法旋轉的問題,因爲每個人幾年一直聲稱它作爲iOS的錯誤。力旋轉的UIViewController

現在我只是需要用力轉動我的UIViewController。有沒有辦法可以做到這一點,因爲實例方法現在已被刪除,我不知道該如何強制旋轉我的控制器。

+0

看看這個問題http://stackoverflow.com/questions/9826920/UINavigationController的強制力旋轉。我不確定,'+(void)attemptRotationToDeviceOrientation'方法可以成爲你的問題的答案。 – Canopus 2012-07-06 18:56:37

+1

它是一件很好的事情要知道,但不幸的是,我的問題是相反的。我沒有試圖旋轉到某個方向,而是在錯誤的方向旋轉,而設備仍然處於錯誤的方向。 – RileyE 2012-07-06 20:16:49

+0

你說'shouldAutoRotateToInterfaceOrientation'沒有被調用。顯然,這是一個預iOS6的問題,所以也許你不關心任何更多的,但如果你仍然有這個問題,這也可以讓視圖控制器層次結構,以獲取不同步您的視圖層次結構造成的(如通過錯誤地使用'addSubview'在視圖之間轉換)。如果您仍然有問題,請隨時通知我們。 – Rob 2013-02-01 18:37:41

回答

8

我不確定您說的哪個方法已被刪除,但以下操作適用於我(這確實使用了 orientation方法)。底線,如果您有一個只接受風景的視圖,則可以使用以下方法強制iOS更改方向。它很好奇,但它很有用。我很抱歉,我不能給原作者給予信貸,但一旦跨越此別處來的StackOverflow:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

- (void)forceLandscape 
{ 
    // make sure shouldAutorotateToInterfaceOrientation is configured to accept only landscape 

    if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) 
    { 
     UIWindow *window = [[UIApplication sharedApplication] keyWindow]; 
     UIView *view = [window.subviews objectAtIndex:0]; 
     [view removeFromSuperview]; 
     [window addSubview:view]; 
    } 
} 

相當於強制肖像模式(假設,當然,你的shouldAutorotateToInterfaceOrientation只接受人像):

- (void)forcePortrait 
{ 
    // make sure shouldAutorotateToInterfaceOrientation is configured to accept only portrait 

    if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) 
    { 
     UIWindow *window = [[UIApplication sharedApplication] keyWindow]; 
     UIView *view = [window.subviews objectAtIndex:0]; 
     [view removeFromSuperview]; 
     [window addSubview:view]; 
    } 
} 
+0

那麼,基本上,刪除根視圖控制器並讀取它?這不看起來有點冒失,因爲控制器不應該旋轉嗎?蘋果是否允許在完整的應用程序中使用? – RileyE 2012-07-06 20:10:58

+0

另外,感謝您的答案!+1 – RileyE 2012-07-06 20:11:15

+0

我只是想等一下,看看別人是否有另一種選擇,但我敢肯定,這是我最終不得不忍受的。 – RileyE 2012-07-06 20:11:51

0

使用xcode7.3 ios9。 我一直在努力,現在強制橫向視圖和關閉了一個多星期,沒有頭髮離開:(對於大家有沒有發現這個問題,沒有答案的工作,或任何其他無數的答案,對強迫景觀,我終於發現的東西,在這裏工作: http://www.btday.com/how-to-force-view-controller-orientation-in-ios-8/

建議是把操作viewDidAppear,但它也似乎在viewWillAppear中工作,並略少醜

override func viewWillAppear(animated: Bool) { 
    UIDevice.currentDevice().setValue(UIInterfaceOrientation.LandscapeLeft.rawValue, forKey: "orientation") 
} 

如果這是錯誤/壞,請講了,只有一點點的Mac過了一個月,所以我總nuub!

另外請注意,我用的UITabBarController,它似乎並不無論你把這個標籤欄控制器或它的一個孩子