仅横向使用多个笔尖的iPhone应用程序 [英] Landscape only iPhone app with multiple nibs

查看:125
本文介绍了仅横向使用多个笔尖的iPhone应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个有几个笔尖的iPhone应用程序,并且应该只是风景。

I'm developing an iPhone application that has several nibs, and should be landscape only.

应用程序设置为通过Info.plist以横向模式启动文件。

The application is set to start in landscape mode via its Info.plist file.

我有两个视图控制器:
FirstViewController SecondViewController

I have two view controllers: FirstViewController and SecondViewController.

对于其中的每一个,我都有一个nib文件,其中视图是横向的。两个视图控制器都作为插件添加到我的 MainView nib中,并且它们的视图被懒惰地初始化。

For each of these I have a nib file, where the view is in landscape. Both view controllers are added to my MainView nib as outlets, and their views are lazily initialized.

当应用程序加载,第一个视图按预期显示在横向中。但是,当我切换到第二个视图时,设备(或模拟器)保持横向,但视图旋转,就好像设备处于纵向模式,制动我的界面。

When the application loads, the first view displays in landscape, as expected. However, when I switch to the second view, the device (or simulator) remains in landscape, but the view is rotated, as if the device were in portrait mode, braking my interface.

UIViewController 类中,我有以下代码:

In both UIViewController classes I have the following code:

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

并切换视图,在我的app委托中我正在做:

and to switch views, in my app delegate I'm doing:

[viewController.view removeFromSuperview];
[window addSubview:secondViewController.view];

其中 viewController secondViewController 是视图控制器连接的两个出口。

where viewController and secondViewController are the two outlets where the view controllers are connected.

这是第二个视图在IB中的显示方式:
< a href =http://img27.imageshack.us/img27/4898/picture1ni.png>替代文字http://img27.imageshack.us/img27/4898/picture1ni.png

This is how the second view looks in IB: alt text http://img27.imageshack.us/img27/4898/picture1ni.png

这就是它在模拟器中的样子:
alt文本http://img402.imageshack.us/img402/4866/picture2wt.png

and this is how it looks in the simulator: alt text http://img402.imageshack.us/img402/4866/picture2wt.png

为什么第二个视图在横向显示,但是界面旋转?

Why is that the second view is displaying in landscape but with the interface rotated?

我不想处理变换属性,因为这似乎有点过分。

I wouldn't like to deal with transform properties, since that seems overkill.

推荐答案

我主演了这个问题,希望有人会给你一个富有洞察力的回应,我会学到一些东西......遗憾的是,我担心你可能需要使用变换才能让这个变得有效佩尔利。这是我最近用来解决问题的代码:

I starred this question hoping someone would give you an insightful response and I'd learn something.. sadly I'm afraid that you might need to use transforms to get this to work properly. Here's the code I've been using lately to solve the problem:

- (void)forceLandscapeForView:(UIView *)theView {
  theView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
  theView.bounds = CGRectMake(0, 0, 480, 320);
  theView.center = CGPointMake(160, 240);
  [theView setNeedsLayout];
  [theView setNeedsDisplay];
}

然后,当您添加新视图时,请检查当前方向以及是否必要强制轮换:

Then when you're adding your new view, check the current orientation and if necessary force the rotation:

if (!UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
  [self forceLandscapeForView:_activeViewController.view];
}

当然你要对 shouldAutorotateToInterfaceOrientation 在每个视图控制器中:

Then of course you'll want to respond appropriately to shouldAutorotateToInterfaceOrientation in each of your view controllers:

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

如果不是全部必要,我很乐意听到备用解决方案。我已经注意到这个设置还有一个警告:如果你在视图之间进行过渡,并且在转换期间旋转手机,则视图方向可能会被翻转或卡住在错误的横向方向上,例如您需要在视图之间导航时将手机翻过来(横向右侧与横向左侧)。

I would love to hear about alternate solutions if this isn't all necessary. There is also one caveat I've noticed with this setup: if you have a transition between views, and you rotate the phone during that transition, it's possible for the views orientations to get flipped or "stuck" on the wrong landscape orientation, such that you need to turn the phone over (landscape-right vs landscape-left) as you navigate between views.

这篇关于仅横向使用多个笔尖的iPhone应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆