chits
chits

Reputation: 21

CGRectMake for different iphone ipad orientations

For following code I need to set different UIIMageView Positions according to iphone and ipad orientation.

// 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)]; 

As CGRect will be different for ipad portrait,iphone portrait, ipad landscape and iphone landscape.

How do I do this

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

Upvotes: 1

Views: 1538

Answers (2)

PengOne
PengOne

Reputation: 48406

You can differentiate between iPhone and iPad with

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

and you can differentiate between portrait and landscape with

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

Now combine this to get the customization as you like.

Upvotes: 0

hundreth
hundreth

Reputation: 841

You can use the UIViewController's interfaceOrientation property. It will give you one of the following values:

UIInterfaceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight

Your code would look like:

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

Upvotes: 3

Related Questions