How to differentiate between 3.5 and 4 inch iPhone

Open Xcode and create a new Single View Application. For product name, use DifferentiateIphoneSize and then fill out the Organization Name and Company Identifier with your customary values. Select iPhone for Devices.



For demonstration purposes, add a label inside the view controller and change the text to "3.5-inch or 4-inch".

We will need to connect the label to the view controller. Select the assistant editor and open the ViewController.m file. Ctrl and drag from the label to the class section and create the following outlet.



Inside the viewDidLoad, add the following lines of code:
 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
if ([[UIScreen mainScreen] scale] == 2.0) {
if([UIScreen mainScreen].bounds.size.height == 568){
// iPhone retina-4 inch
self.lbl_textOutput.text = @"iPhone retina-4 inch";
} else{
// iPhone retina-3.5 inch
self.lbl_textOutput.text = @"iPhone retina-3.5 inch";
}
}
else {
// not retina display
self.lbl_textOutput.text = @"iPhone that's not retina display";
}
}

Build and Run, a label can be seen with text "iPhone retina-4 inch" if the app is run in with 4-inch size, otherwise "iPhone retina-3.5 inch".


You can download the source code of the DifferentiateIphoneSize at my repository on bitbucket.

0 nhận xét:

Đăng nhận xét