2009-02-08 66 views
4

我正尝试使用实用程序模板创建始终处于横向模式的iPhone应用程序。下面是我所做的:iPhone横向专用实用程序 - 模板应用程序

  • 创建一个新的iPhone应用程序项目,使用实用工具应用程序模板
  • 在Interface Builder中,旋转各方面的意见90度。
  • 在Interface Builder中,向MainView的中间添加一个标签。在整个视图中一直拉伸,将对齐设置为居中,并设置自动弹簧,使其可以水平拉伸。
  • 在Info.plist中,添加键 “UIInterfaceOrientation” 与值 “UIInterfaceOrientationLandscapeRight”
  • 在控制器类,改变shouldAutorotateToInterfaceOrientation方法 “返回(interfaceOrientation == UIInterfaceOrientationLandscapeRight)||(interfaceOrientation == UIInterfaceOrientationLandscapeLeft);”
  • 运行该应用程序。

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

我已经能够通过将以下步骤上述以解决该问题:

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

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

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

如果我使用基于视图的应用程序模板而不是实用程序来执行完全相同的一组步骤,则一切都按预期工作。所以我很确定这个问题是由一个Utility应用程序管理其视图的具体情况决定的。

有人明白这里发生了什么?

+0

一种愚蠢的问题,但是在IB查看正确的大小? – 2009-02-08 14:10:39

+0

是的,大小是正确的。 – 2009-02-08 16:44:22

回答

3

我想说设置这个键不会旋转你的界面;您仍然需要在横向模式下布局内容并使用CFAffineTransform进行适当的旋转 - 请参阅iPhone OS编程指南中的“在横向模式下启动”。为了找到你的参考资料,我发现了这样一条评论:“要在v2.1之前的iPhone OS版本中以横向模式启动基于视图控制器的应用程序,需要将90度旋转应用于应用程序的转换在iPhone OS 2.1之前,视图控制器不会根据UIInterfaceOrientation键的值自动旋转它们的视图,但这一步在iPhone OS 2.1和更高版本中不是必需的。

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

-(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;  
} 
2

简描述UIInterfaceOrientation至UIInterfaceOrientationLandscapeRight(或UIInterfaceOrientationLandscapeLeft),以及在文档中所建议的旋转设置的设定,但我在我的根视图控制器使用的代码稍微不同的块(以相同的端部) :

- (void)loadView 
{ 
    UIView *primaryView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    primaryView.backgroundColor = [UIColor clearColor]; 

    // Start in landscape orientation, and stay that way 
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    if (orientation == UIInterfaceOrientationLandscapeRight) 
    { 
     CGAffineTransform transform = primaryView.transform; 

     // Use the status bar frame to determine the center point of the window's content area. 
     CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; 
     CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x); 
     CGPoint center = CGPointMake(60.0, bounds.size.height/2.0); 

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

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

    self.view = primaryView; 
    [primaryView release]; 
} 

除此之外,我在根视图控制器中实现的以下委托方法:

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

最后,我遇到与模拟器不会自动旋转正确怪异的毛刺,所以我需要实现我的UIApplicationDelegate以下的委托方法:

- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration; 
{ 
    // This prevents the view from autorotating to portrait in the simulator 
    if ((newStatusBarOrientation == UIInterfaceOrientationPortrait) || (newStatusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) 
     [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; 
} 

毕竟,我的应用程序能够在横向启动(右键)并保持在2.0固件和模拟器下的方向。

0

尝试将视图的方向属性设置为笔尖中的横向。该属性可以在模拟矩阵下的UIView的信息视图的第4个选项卡[属性检查器]中找到。