Question

I am setting my iphone app screen frames to support iphone 5 , i have set the auto resizing mask mask and also i tried by setting the frame programmatically using the following code in loadview

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.height == 480)
        {
            // iPhone Classic
        }
        if(result.height > 480)
        {
            // iPhone 5
           CGRect mainFrame = CGRectMake(0, 0, 320, 568);
           self.view.frame = mainFrame;
        }
    }

but it didnt work didn't work ,

can any one tell me how do i set the frame , thanx in advance

Was it helpful?

Solution

Use this to check :

#define IS_IPHONE ( [[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define IS_IPHONE_5 ( IS_IPHONE && IS_HEIGHT_GTE_568 )
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

So

if(IS_IPHONE_5)
{
 // Iphone 5 frame
}
else
{
// iphone 4 frame

}

and add Default-568h@2x.png in your project also.

Hope it helps you.

OTHER TIPS

Your code doesn't work for one of the below reason

1) If you rotate device , check your main screen bounds. It may lead to confusion. You can try Better way to use it.

2) This Default-568h.png is only allowed when building an app using Xcode 4.5 and the iOS 6 SDK

3) When you ask a UIScreen for it's Bounds you get the bounds of the screen, which is the whole device screen. (the status bar is part of the screen)

So It may get false two if conditions.

You can use this code ..

-(BOOL)isDeviceGreaterThaniPhone5
{
    struct utsname systemInfo;
    uname(&systemInfo);

    NSString *modelName = [NSString stringWithCString:systemInfo.machine
                                             encoding:NSUTF8StringEncoding];
    if([modelName isEqualToString:@"iPhone5,1"]) {// @"iPhone 5 (GSM)";
        return YES;
    }
    else if([modelName isEqualToString:@"iPhone5,2"]) {// @"iPhone 5 (GSM+CDMA)";
        return YES;
    }
    else if([modelName isEqualToString:@"iPod5,1"]){ // @"iPod touch 5G";
        return YES;
    }
    return NO;
}

Don't forget to import #import <sys/utsname.h>

try like this,

if ( [[UIScreen mainScreen] bounds].size.height == 568 ) {

//iphone5 frame

}
else{
//not an iphone5 frame
}

for details check this one

Response from Nishant is great, Here I am just providing a consized version of this, as you know height is only difference, hence pass height for your frame in both conditions:

Here is a macro:

#define ASSET_BY_SCREEN_HEIGHT(regular, longScreen) (([[UIScreen mainScreen] bounds].size.height <= 480.0) ? regular : longScreen)

And Here how to use it:

CGRect frame = CGRectMake(0, 0, 320, ASSET_BY_SCREEN_HEIGHT(460, 548));

It doesn't work because you should add your view as subview and wait until it will be drawn.

For my project I solved this by putting a code which depends on screen size after autosizing into drawRect: for UIViews and into viewDidAppear for UIViewControllers.

So if you use a correct autoresizing mask then you may not make your view longer: Autoresizing masks programmatically vs Interface Builder / xib / nib

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top