【问题标题】:iPhone Landscape-Only Utility-Template ApplicationiPhone Landscape-Only Utility-Template 应用程序
【发布时间】:2010-10-06 06:14:27
【问题描述】:

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

  • 使用 Utility Application 模板创建一个新的 iPhone 应用程序项目
  • 在 Interface Builder 中,将所有视图旋转 90 度。
  • 在 Interface Builder 中,将标签添加到 MainView 的中间。在视图中一直拉伸它,将对齐设置为居中,并设置自动调整大小的弹簧,使其可以水平拉伸。
  • 在 Info.plist 中,添加值为“UIInterfaceOrientationLandscapeRight”的键“UIInterfaceOrientation”
  • 在控制器类中,将 shouldAutorotateToInterfaceOrientation 方法更改为“return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);”
  • 运行应用程序。

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

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

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

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

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

如果我使用基于视图的应用程序模板而不是实用程序执行完全相同的一组步骤,那么一切都会按预期工作。所以我很确定问题是特定于实用程序应用程序如何管理其视图。

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

【问题讨论】:

标签: iphone landscape


【解决方案1】:

我要说的是,设置这个键不会旋转你的界面;您仍然需要以横向模式布置内容并使用 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;    
}

【讨论】:

  • 我使用的是 iPhone OS 2.2.1。我尝试了您建议的转换,但没有任何改变。 FWIW,我不认为是变换,因为所有内容都以正确的方向和适当的缩放比例显示,但似乎所有内容的边界矩形都已关闭。
【解决方案2】:

Jane 描述了 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 固件和模拟器中保持该方向。

【讨论】:

    【解决方案3】:

    尝试在笔尖中将视图的方向属性设置为横向。该属性可以在 Simulated Metrices 下 UIView 的 Info View 的 4th tab[Attributes Inspector] 中找到。

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 1970-01-01
      • 2014-09-19
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      相关资源
      最近更新 更多