无法将界面方向旋转为纵向倒置

时间:2012-12-27 16:23:27

标签: iphone ios6 storyboard portrait interface-orientation

在iPhone模拟器和iPhone 3GS(iOS 6)上,我都无法将方向设置为纵向颠倒。我只有一个ViewController。我在其中添加了此代码:

-(BOOL) shouldAutorotate{
 return YES; 
}

-(UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
 return UIInterfaceOrientationPortraitUpsideDown;
}

- (NSUInteger)supportedInterfaceOrientations{
 return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown;
}

-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
  if (toInterfaceOrientation==UIInterfaceOrientationPortrait || toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
    return YES;
  }
 return NO;
}

我还将此添加到AppDelegate.m:

-(NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown;
}

我还检查了plist文件,两个方向都存在。在我的故事板上,在Simulated Metrics Orientation设置为Inferred。我在applicationDidFinishLaunchingWithOptions中什么都不做,除了返回YES。我刚刚在这个ViewController中添加了一个UIImageView。是否有可能使这种方向有效?

3 个答案:

答案 0 :(得分:11)

supportedInterfaceOrientations会返回NSUInteger。它返回视图控制器支持的所有接口方向,即接口方向值的掩码。

您需要返回UIInterfaceOrientationMask枚举中定义的值,如下图所示:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}

答案 1 :(得分:1)

我知道有一个答案可行,但对于仍然遇到同样问题的其他人来说:

我遇到了与Xcode相关的类似问题:尽管在所有情况下都将YES返回- (BOOL) shouldAutorotateToInterfaceOrientation:,但仍未支持纵向旋转。结果是我的目标项目编辑器的摘要窗口中启用的“支持的界面方向”:

enter image description here

上面的“iPhone / iPad Depoloyment Info”选项没有这样做,正是“iPad部署信息”部分似乎可以控制模拟器将会做什么,尽管事实上我只使用iPhone sim。一旦我在该部分启用了相同的方向,那么iPhone模拟就可以工作了,我能够测试模拟器旋转时发生的事情....

答案 2 :(得分:0)

我尝试了很多方法。它似乎只有一种方式可行,但全局通过应用程序,不仅适用于特定的视图控制器。

首先,您必须在目标常规设置中检查设备方向的颠倒。

check Upside Down of Device Orientation

然后,在导航控制器扩展中,您将覆盖supportedInterfaceOrientations方法

extension UINavigationController {
    override public func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .All
    }
}
相关问题