CGRectMake用于不同的iphone ipad方向

时间:2011-06-20 17:02:45

标签: iphone ios4 orientation cgrect

对于以下代码,我需要根据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)]; 

因为CGRect对于ipad肖像,iphone肖像,ipad风景和iphone风景将有所不同。

我该怎么做

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 (...............)
}

2 个答案:

答案 0 :(得分:3)

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

UIInterfaceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight

您的代码如下:

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

答案 1 :(得分:0)

您可以使用

区分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.
}

现在将其组合起来以获得您喜欢的自定义。