Question

I am developing an app, and I want it can be used on iPhone 4 and 5, as the iPhone 5 had changed its size to 4-inch, so I want my app can be supported 4-inch, but in Xcode4.3 there is no autolayout, because of this , I wonder if there is anyway to get the display screen size and change it to 4-inch, thank you very much!

Was it helpful?

Solution

You can get the screen size by using this code:

CGRect screenBounds = [[UIScreen mainScreen] bounds];
UIViewController *ViewController;
[self loadall];
if (screenBounds.size.height == 568) {
    NSLog(@"User is using an iPhone 5+");
    BOOL isUsingiPhone5 = YES;
}
else{
    NSLog(@"User is using an iPhone 4s-");
    BOOL isUsingiPhone5 = NO;
}

then you could move things accordingly, with CGRect heres an example of moving an ad banner view as a result of the screen size:

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
CGRect bannerFrame = banner.frame;
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UIViewController *ViewController;
if (screenBounds.size.height == 568) {
    //iPhone5+
    bannerFrame.origin.x = 0;
    bannerFrame.origin.y = 518;
    banner.frame = bannerFrame;
}
else{
    //iPhone4s-
}
NSLog(@"Showing ad, internet connection found.");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
else{
    [banner setAlpha:0];
}

Make sure to use ViewController as the UIViewController variable, even if your default view controller is named something like RootViewController.

Hope this helps. Feel free to comment if you have any questions!

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