2012-07-06 63 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,它似乎并不无论你把这个标签栏控制器或它的一个孩子