iPhone仅景观实用程序 - 模板应用程序 [英] iPhone Landscape-Only Utility-Template Application

查看:76
本文介绍了iPhone仅景观实用程序 - 模板应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Utility应用程序模板创建一个始终处于横向模式的iPhone应用程序。这就是我的所作所为:




  • 使用效用应用程序模板创建一个新的iPhone应用程序项目

  • 在Interface Builder中,将所有视图旋转90度。

  • 在Interface Builder中,将标签添加到MainView的中间。在视图中一直拉伸,将对齐设置为居中,并设置自动调整弹簧,使其可以水平拉伸。

  • 在Info.plist中,添加键UIInterfaceOrientation值UIInterfaceOrientationLandscapeRight

  • 在控制器类中,将shouldAutorotateToInterfaceOrientation方法更改为return(interfaceOrientation == UIInterfaceOrientationLandscapeRight)||(interfaceOrientation == UIInterfaceOrientationLandscapeLeft);

  • 运行应用程序。



当我启动我的应用程序时,它以横向方向显示,但主视图仅覆盖显示屏的上半部分,并且它是水平拉伸的。我在模拟器和实际设备上都得到了相同的结果。我已经在SDK的2.2和2.2.1版本中看到了它。



我已经能够通过在上面添加以下步骤来解决这个问题:




  • 添加self.view.autoresizesSubviews = NO; [super viewDidLoad];之后的RootViewController的viewDidLoad方法。



如果我这样做,那么它按预期工作。但这感觉就像一个黑客。为什么这有必要?



我不认为这是转型问题。所有元素都以正确的方向绘制并具有适当的缩放比例。问题似乎是主视图的边界矩形变得很时髦。看起来主视图的高度被削减了一半多一点,宽度增加了大约50%。



如果我这样做的话使用基于视图的应用程序模板而不是实用程序的相同步骤集,然后一切都按预期工作。所以我很确定问题是针对Utility应用程序如何管理其视图的。



有人知道这里发生了什么吗?

解决方案

我打算说设置这个键不会旋转你的界面;您仍然需要以横向模式布置内容并使用CFAffineTransform进行适当的旋转 - 请参阅iPhone OS编程指南中的在横向模式下启动。为了找到你的参考,我发现这个评论:要在v2.1之前的iPhone OS版本中以横向模式启动基于视图控制器的应用程序,你需要对应用程序的转换应用90度旋转。除了上述所有步骤之外,还有根视图。在iPhone OS 2.1之前,视图控制器没有根据UIInterfaceOrientation键的值自动旋转视图。但是,在iPhone OS 2.1及更高版本中,此步骤不是必需的。 p>

因此,如果您在2.1之前运行,则需要将此代码添加到视图控制器中的viewDidLoad方法。 (否则,你可以发布一些代码吗?)

   - (void)viewDidLoad 
//加载视图后,转换视图以使坐标适合于景观
//如iPhone应用程序编程指南中所述
//很奇怪,我确信过去需要它,但它现在不需要。但是需要CardScrollViewController中的那个。
{
[super viewDidLoad];

CGAffineTransform transform = self.view.transform;
CGPoint center = CGPointMake(kScreenHeight / 2.0,kScreenWidth / 2.0);

//将视图的中心点设置为窗口内容区域的中心点。
self.view.center = center;

//围绕新的中心点旋转视图90度。
transform = CGAffineTransformRotate(transform,(M_PI / 2.0));
self.view.transform = transform;
}


I'm trying to create an iPhone application that is always in landscape mode, using the Utility application template. Here's what I did:

  • Create a new iPhone application project, using the Utility Application template
  • In Interface Builder, rotate all the views 90 degrees.
  • In Interface Builder, add a label to the middle of the MainView. Stretch it all the way across the view, set the alignment to centered, and set the autosizing springs so that it can stretch horizontally.
  • In Info.plist, add the key "UIInterfaceOrientation" with value "UIInterfaceOrientationLandscapeRight"
  • In the controller classes, change the shouldAutorotateToInterfaceOrientation methods to "return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);"
  • Run the app.

When I launch my app, it comes up in landscape orientation, but the main view only covers the top half of the display, and it is stretched horizontally. I get the same results in both the simulator and on an actual device. I've seen it with versions 2.2 and 2.2.1 of the SDK.

I have been able to work around the problem by adding the following step to the above:

  • Add "self.view.autoresizesSubviews = NO;" to RootViewController's viewDidLoad method after "[super viewDidLoad];".

If I do this, then it works as expected. But this feels like a hack. Why should this be necessary?

I don't think it is a transformation issue. All elements are drawn in the proper orientation and with the proper scaling. The problem seems to be that the bounds rectangles of the main view gets funky. It looks like the height of the main view is being cut by a little more than half, and the width is being increased by about 50%.

If I do the exact same set of steps using the View-based Application template instead of Utility, then everything works as expected. So I'm pretty sure the problem is specific to how a Utility application manages its views.

Anybody understand what's going on here?

解决方案

I was going to say that setting this key does not rotate your interface; you still need to lay out your content in landscape mode and do the appropriate rotation using CFAffineTransform - see "Launching in Landscape Mode" in iPhone OS Programming Guide. Going to find the reference for you, I found this comment: "To launch a view controller–based application in landscape mode in versions of iPhone OS prior to v2.1, you need to apply a 90 degree rotation to the transform of the application’s root view in addition to all the preceding steps. Prior to iPhone OS 2.1, view controllers did not automatically rotate their views based on the value of the UIInterfaceOrientation key. This step is not necessary in iPhone OS 2.1 and later, however."

So if you're running pre-2.1, you need to add this code to your viewDidLoad method in your view controller. (Otherwise, can you post some code?)

-(void)viewDidLoad 
// After loading the view, transform the view so that the co-ordinates are right for landscape
// As described in iPhone Application Programming Guide
// Weird, I'm sure this used to be needed, but it doesn't now. The one in CardScrollViewController is needed though.
{
    [super viewDidLoad];

    CGAffineTransform transform = self.view.transform;
    CGPoint center = CGPointMake(kScreenHeight / 2.0, kScreenWidth / 2.0);

    // Set the center point of the view to the center point of the window's content area.
    self.view.center = center;

    // Rotate the view 90 degrees around its new center point.
    transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
    self.view.transform = transform;    
}

这篇关于iPhone仅景观实用程序 - 模板应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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