簡體   English   中英

CGRectMake適用於不同的iPhone iPad方向

[英]CGRectMake for different iphone ipad orientations

對於以下代碼,我需要根據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 (...............)
}

您可以使用UIViewController的interfaceOrientation屬性。 它會為您提供以下值之一:

UIInterfaceOrientationPortrait,UIInterfaceOrientationPortraitUpsideDown,UIInterfaceOrientationLandscapeLeft,UIInterfaceOrientationLandscapeRight

您的代碼如下所示:

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

您可以通過以下方式區分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.
}

現在,將其結合起來即可根據需要獲得自定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM