Jawaban:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
Itu akan memberi Anda resolusi seluruh layar dalam poin, sehingga biasanya 320x480 untuk iPhone. Meskipun iPhone4 memiliki ukuran layar yang jauh lebih besar, iOS masih memberikan 320x480, bukan 640x960. Ini sebagian besar karena melanggar aplikasi yang lebih lama.
CGFloat screenScale = [[UIScreen mainScreen] scale];
Ini akan memberi Anda skala layar. Untuk semua perangkat yang tidak memiliki Tampilan Retina ini akan mengembalikan 1.0f, sedangkan perangkat Retina Display akan memberikan 2.0f dan iPhone 6 Plus (Retina HD) akan memberikan 3.0f.
Sekarang jika Anda ingin mendapatkan lebar piksel & tinggi layar perangkat iOS Anda hanya perlu melakukan satu hal sederhana.
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
Dengan mengalikan dengan skala layar Anda mendapatkan resolusi piksel yang sebenarnya.
Bacaan yang bagus tentang perbedaan antara titik dan piksel di iOS dapat dibaca di sini .
EDIT: (Versi untuk Swift)
let screenBounds = UIScreen.main.bounds
let screenScale = UIScreen.main.scale
let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)
Kelas UIScreen memungkinkan Anda menemukan resolusi layar dalam Points and Pixels.
Resolusi layar diukur dalam Poin atau Piksel. Seharusnya tidak pernah bingung dengan ukuran layar. Ukuran layar yang lebih kecil dapat memiliki resolusi lebih tinggi.
'Bounds.width' UIScreen mengembalikan ukuran persegi panjang di Points
'NativeBounds.width' UIScreen mengembalikan ukuran persegi panjang dalam Pixel. Nilai ini dideteksi sebagai PPI (Point per inch). Menunjukkan ketajaman & kejelasan Gambar pada perangkat.
Anda dapat menggunakan kelas UIScreen untuk mendeteksi semua nilai ini.
Swift3
// Normal Screen Bounds - Detect Screen size in Points.
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
print("\n width:\(width) \n height:\(height)")
// Native Bounds - Detect Screen size in Pixels.
let nWidth = UIScreen.main.nativeBounds.width
let nHeight = UIScreen.main.nativeBounds.height
print("\n Native Width:\(nWidth) \n Native Height:\(nHeight)")
Menghibur
width:736.0
height:414.0
Native Width:1080.0
Native Height:1920.0
Swift 2.x
//Normal Bounds - Detect Screen size in Points.
let width = UIScreen.mainScreen.bounds.width
let height = UIScreen.mainScreen.bounds.height
// Native Bounds - Detect Screen size in Pixels.
let nWidth = UIScreen.mainScreen.nativeBounds.width
let nHeight = UIScreen.mainScreen.nativeBounds.height
ObjectiveC
// Normal Bounds - Detect Screen size in Points.
CGFloat *width = [UIScreen mainScreen].bounds.size.width;
CGFloat *height = [UIScreen mainScreen].bounds.size.height;
// Native Bounds - Detect Screen size in Pixels.
CGFloat *width = [UIScreen mainScreen].nativeBounds.size.width
CGFloat *height = [UIScreen mainScreen].nativeBounds.size.width
Gunakan di App Delegate: Saya menggunakan storyboard
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
//----------------HERE WE SETUP FOR IPHONE 4/4s/iPod----------------------
if(iOSDeviceScreenSize.height == 480){
UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone35Storyboard instantiateInitialViewController];
// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController = initialViewController;
// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
iphone=@"4";
NSLog(@"iPhone 4: %f", iOSDeviceScreenSize.height);
}
//----------------HERE WE SETUP FOR IPHONE 5----------------------
if(iOSDeviceScreenSize.height == 568){
// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"iPhone5" bundle:nil];
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController];
// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController = initialViewController;
// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
NSLog(@"iPhone 5: %f", iOSDeviceScreenSize.height);
iphone=@"5";
}
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// NSLog(@"wqweqe");
storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil];
}
return YES;
}
Lihat Referensi UIScreen: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html
if([[UIScreen mainScreen] respondsToSelector:NSSelectorFromString(@"scale")])
{
if ([[UIScreen mainScreen] scale] < 1.1)
NSLog(@"Standard Resolution Device");
if ([[UIScreen mainScreen] scale] > 1.9)
NSLog(@"High Resolution Device");
}
NSLog(@"%f",[[UIScreen mainScreen] scale]);
Gunakan kode ini untuk membantu mendapatkan resolusi layar perangkat apa pun
[[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width