【问题标题】:xcode ipad landscape orientation - reposition objectsxcode ipad横向 - 重新定位对象
【发布时间】:2013-03-29 07:25:27
【问题描述】:

试图让我的 iPad 应用程序以横向模式运行。我想在横向模式下更改一些控件的位置。我得到了日志,所以我知道我在方法中,但是,我的对象都没有移动。我错过了什么吗?

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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(toInterfaceOrientation==UIInterfaceOrientationLandscapeRight ||
   toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft)
    {
        NSLog(@"in landscape");
        // I would like to re-position my controls here but not working
        image1.frame = CGRectMake(318, 501, 100, 100);
        btnNext.frame = CGRectMake(200, 455, 100, 35)
    }
}

【问题讨论】:

  • 我在我的 iPad 模拟器 5.0 上对此进行了测试,它在这里工作。看起来这只是较新的iOS的问题。我该如何处理?使用故事板,所以我想支持 5.0 及更高版本。建议?

标签: xcode ipad orientation landscape


【解决方案1】:

您的代码适用于 iOS 5,但对于 iOS 6 及更高版本,请尝试使用方向掩码。这是一种方法,您可以在其中实现对象的旋转。

- (BOOL) shouldAutorotate
{
return YES;
 }

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

您可以移除不需要的掩码。在 iOS 6 中,通过在此方法中输入您想要的对象后,您应该归档您正在尝试执行的操作。

【讨论】:

  • 好的,这有帮助!但是我怎么知道我的应用程序是在哪个方向启动的呢?如果以横向打开,则视图显示为纵向。
  • 如果你想在一个方向上设置应用程序的方向,有两个方法可以做到。一种是在信息页面上的项目目标级别,只是取消突出显示您不想要的视图方向。二是通过您已经拥有的代码。同样在情节提要中,您可以将视图设置为横向或纵向。如果你想要横向,然后突出显示并使用掩码代码,它会做到的。希望这有助于清除它。如果我能以任何方式帮助您,请告诉我。
【解决方案2】:

尝试将您的重新定位代码移至viewWillLayoutSubviews

如果您真的想一路走下去,请仅在 iOS 6 中使用约束。您的 iOS 6 代码将与您的 iOS 5 代码非常、非常不同,这很烦人,但它也会更加优雅。在我书中的这个讨论中,我深入探讨了重新排列界面以响应旋转的问题:

http://www.apeth.com/iOSBook/ch19.html#SECrotationevents

我在那次讨论中提出的一个优雅的解决方案是首先使用约束设置界面,然后实现updateViewConstraints 作为调整这些约束以响应旋转的地方。然后讨论继续展示如何在应用程序以横向启动的情况下使用viewWillLayoutSubviews 来设置initial 界面;但同样,正如它随后指出的那样,如果您可以使用约束来代替,那就更好了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 2011-04-11
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多