【问题标题】:orientation issue - ipad方向问题 - ipad
【发布时间】:2012-07-24 20:37:35
【问题描述】:

我的应用程序有几个 uiViews 并设置为支持两种方向。因为我的 uiViews 框架只是 iPad 整个尺寸的一部分,所以我试图根据 iPad 的固定方式来使我的 uiviews 框架居中。在 view controller.m 文件中以这种方式完成:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    UIInterfaceOrientation des=self.interfaceOrientation;

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) //iPad
    {
        CGRect ScreenBounds  = [[UIScreen mainScreen] bounds];

        if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown)//ipad-portrait
        {

            ipStPosX=(ScreenBounds.size.width -360)/2;
            ipStPosY=100; 
            return YES;
        }
        else//ipad -landscape
        {
            ipStPosX=(ScreenBounds.size.height -360)/2;
            ipStPosY=100; 
            return YES;
        }
    }
    else//iphone
    {
        UIInterfaceOrientation des=self.interfaceOrientation;

        if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown) //iphone portrait
        {
            return NO;
        }
        else //iphone -landscape
        {
            return YES;
        }
    }
}

在启动应用程序并更改方向后,无论我如何握住设备,它都会始终转到纵向部分 UIInterfaceOrientationPortrait。

我看到一些旧帖子并不真正适合这个问题并且令人困惑或过时,所以这里的任何帮助都会很棒。

【问题讨论】:

    标签: iphone objective-c ios xcode ipad


    【解决方案1】:

    shouldAutorotateToInterfaceOrientation: 只是告诉 iOS 你支持特定的方向。既然你想要除了 iPhone 风景之外的所有东西,你应该返回

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //iPad
            return YES;
        return UIInterfaceOrientationIsPortrait(orientation);
    }
    

    要实际调整显示的内容,您想使用willRotateToInterfaceOrientation:duration:didRotateFromInterfaceOrientation: 来实际更改项目。在你的情况下,鉴于你只是根据你所处的方向调整一些 iVar,我想你会使用后者。

    - (void)didRotateToInterfaceOrientation:(UIInterfaceOrientation)fromOrientation
        UIInterfaceOrientation des = self.interfaceOrientation;
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //iPad
        {
            CGRect ScreenBounds  = [[UIScreen mainScreen] bounds];
    
            if (UIInterfaceOrientationIsPortrait(des))//ipad-portrait
            {
                ipStPosX=(ScreenBounds.size.width -360)/2;
                ipStPosY=100; 
            }
            else //ipad -landscape
            {
                ipStPosX=(ScreenBounds.size.height -360)/2;
                ipStPosY=100; 
            }
        }
    }
    

    【讨论】:

    • 我正在做的是改变 UIView 框架的初始位置,例如: self.svDGrid.frame = CGRectMake(1+ipStPosX, 271*scaleY, 320*scaleY,200*scaleY );我不熟悉 didAutorotateToInterfaceOrientation:duration 方法我可以使用与我为 shouldAutorotateToInterfaceOrientation 发布的代码相同的代码吗?似乎不起作用。如果您可以详细说明或分享一些示例,将对我有所帮助
    • 正是如此。为此,请使用 willAutorotateToInterfaceOrientation:duration:didAutoRotateToInterfaceOrientation:duration: 。但要让它适用于所有方向,您仍然需要在 shouldAutorotateToInterfaceOrientation: 中回答 return YES;
    • 好的,我已经更新了我的答案,为了清楚起见,合并了你的代码)
    • 如上所述,我使用了 didRotateToInterfaceOrientation 并且我将 iPad 从纵向更改为横向,但并未触发此方法。如果我使用 willRotateToInterfaceOrientation:duration 代替,它将进入方法内部,但它再次认为它是肖像,无论我如何拿着 iPad .. 嗯有什么想法吗?
    • 我是否正确理解上面的代码是针对特定视图控制器的?如果您没有将 shouldAutorotateToInterfaceOrientation: 设置为在整个代码中以这种方式工作,那么其中一位父母可能会阻止其中一位孩子旋转。
    【解决方案2】:

    我认为这里有两个问题:

    1. 启用轮换 - 这可以通过实施以下方法来完成。您应该在此方法中放置一个断点以确保它被调用并且您返回 YES。请注意,此方法仅对推送到导航栏或标签栏或属于窗口一部分的视图调用。对于已使用 addSubView 方法添加到其他视图的视图,将不会调用此方法。

      • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)方向;
    2. 实施上述方法而不实施任何其他方法将旋转您的视图。如果没有,您需要调查我上面概述的要点。一旦您的视图开始旋转,您就需要使用自动调整大小的蒙版来实现您的计划。使用自动调整大小的蒙版应该可以轻松实现视图居中。如果使用 XIB,您可以在 xcode 中查看调整大小的行为。

    【讨论】:

      猜你喜欢
      • 2013-03-14
      • 2011-10-24
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 2011-03-13
      相关资源
      最近更新 更多