【问题标题】:CGRectMake for different iphone ipad orientationsCGRectMake 用于不同的 iphone ipad 方向
【发布时间】:2011-06-20 17:02:45
【问题描述】:

对于以下代码,我需要根据 iphone 和 ipad 方向设置不同的 UIIMageView 位置。

// Animated images - centered on screen
animatedImages = [[UIImageView alloc] initWithFrame:CGRectMake(
(SCREEN_WIDTH / 2) - (IMAGE_WIDTH / 2), 
(SCREEN_HEIGHT / 2) - (IMAGE_HEIGHT / 2) + STATUS_BAR_HEIGHT,
IMAGE_WIDTH, IMAGE_HEIGHT)]; 

因为 ipad 纵向、iphone 纵向、ipad 横向和 iphone 横向的 CGRect 会有所不同。

我该怎么做

if (Ipad Portrait orientation)
{ 
code with different position using CGRectMake (...............)
}
if (Iphone Portrait orientation)
{ 
code with different position using CGRectMake (...............)
}
if (Ipad Landscape orientation)
{ 
code with different position using CGRectMake (...............)
}
if (Iphone Landscape orientation)
{ 
code with different position using CGRectMake (...............)
}

【问题讨论】:

  • 请格式化您的帖子,并使用代码标签,以便阅读。

标签: iphone ios4 orientation cgrect


【解决方案1】:

您可以使用 UIViewController 的 interfaceOrientation 属性。它将为您提供以下值之一:

UIInterfaceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight

您的代码如下所示:

if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
{
    ...
}
else if    // etc.

【讨论】:

    【解决方案2】:

    你可以区分 iPhone 和 iPad

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
         // The device is an iPad running iPhone 3.2 or later.
    }
    else
    {
         // The device is an iPhone or iPod touch.
    }
    

    你可以区分纵向和横向

    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
         // The device is in landscape.
    }
    else
    {
         // The device is in portrait.
    }
    

    现在结合它来获得你喜欢的自定义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2012-09-14
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多