如何在横向方向上推出通用的Xcode 5 app项目?

时间:2013-11-23 12:19:42

标签: ios iphone ipad uikit xcode5

我创建了一个“单一视图应用程序”项目,并设置了所有选项以启动并仅支持横向。但该应用程序以纵向方向启动窗口。

控制台输出表明application:didFinishLaunchingWithOptions:之后的窗口是:

{{0, 0}, {768, 1024}}

我添加了一个标准系统按钮来查看ViewController,以说明应用程序是横向而不是纵向,但窗口是纵向的。

因为堆栈溢出具有白色背景,所以我在Photoshop中将右侧部分的颜色变为灰色,以便您可以看到它:

enter image description here

application:didFinishLaunchingWithOptions:中,我将窗口涂成红色。它显然不是横向的。

我尝试过所有我知道的技巧:

1)Info.plist包含UISupportedInterfaceOrientations键,其中包含: UIInterfaceOrientationLandscapeLeft和UIInterfaceOrientationLandscapeRight

2)Info.plist包含UISupportedInterfaceOrientations~ipad键: UIInterfaceOrientationLandscapeLeft和UIInterfaceOrientationLandscapeRight

3)Info.plist包含初始界面方向的UIInterfaceOrientation键: UIInterfaceOrientationLandscapeRight

4)点击Xcode中的项目,然后取消选中所有肖像选项,只检查横向选项:

enter image description here

5)AppDelegate和ViewController都有:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

- (BOOL)shouldAutorotate {
    return YES;
}

6)AppDelegate有:

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

Xcode创建了两个故事板文件。我现在只在iPad上测试。一个是Main_iPad.storyboard,它显示了一个纵向的视图控制器。但它只是一个模拟的界面指标。改为横向,正如预期没有效果。

enter image description here

在应用程序启动期间,我检查了UIScreen边界,它也显然是纵向,而不是横向:

CGRect screenBounds = [[UIScreen mainScreen] bounds]; // {{0, 0}, {768, 1024}}

如何让它以横向方式启动并以横向方向创建窗口?

2 个答案:

答案 0 :(得分:1)

之前我遇到过类似的问题,所以也许他们有关系......

我遇到了一个问题,因为我在视图控制器的initWithNibName:bundle:中以编程方式创建了我的视图,无论出于何种原因,这样做会在纵向模式下定位新视图,而应用程序的其余部分正在运行横向。

我发现了这个问题的两个修复程序。 1)使用故事板而不是以编程方式创建视图或2)移动代码以从视图控制器的initWithNibName:bundle:方法创建视图,并将其放在视图控制器的-viewDidLoad:方法中,如下所示:

-(void)viewDidLoad {
    //Create your view programmatically
    UIView *view = [[UIView alloc] initWithFrame:self.view.frame];
    //Customize view
    [self.view addSubview:view];
}

这是我遇到类似问题的解决方案。虽然我认识到有很多方法可以让程序员无法正常工作,但我希望这就是答案!

答案 1 :(得分:0)

在界面制作工具中点击查看控制器,然后在尺寸检查器中将模拟尺寸切换为自由形式(从固定),为宽度输入1024,为高度输入768。它对我有用。