2010-09-03 47 views

Trả lời

88

Bạn có thể sử dụng bounds tài sản trên một thể hiện của UIScreen:

CGRect screenBound = [[UIScreen mainScreen] bounds]; 
CGSize screenSize = screenBound.size; 
CGFloat screenWidth = screenSize.width; 
CGFloat screenHeight = screenSize.height; 

Hầu hết mọi người sẽ muốn màn hình chính; nếu bạn có một thiết bị gắn liền với TV, bạn có thể muốn lặp lại qua UIScreens và nhận được giới hạn cho mỗi thiết bị.

Thông tin thêm tại số UIScreen class docs.

+0

Đó là công việc, cảm ơn phản hồi của bạn. – haisergeant

+1

Mã này không hoạt động nếu màn hình là màn hình hiển thị võng mạc - cần tính đến [giá trị tỷ lệ] (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScreen_Class/Reference/ UIScreen.html # // apple_ref/occ/instp/UIScreen/scale). –

+0

Điều đó tùy thuộc vào định nghĩa về kích thước của bạn. Các giá trị trả về là các điểm, thường là đơn vị mong muốn khi xử lý, ví dụ, một hệ thống phân cấp khung nhìn. Nếu bạn cần kích thước pixel, có, yếu tố tỷ lệ đi vào chơi; trong kinh nghiệm của tôi, tuy nhiên, đây là một nhu cầu hiếm hơn. – Tim

1

Sử dụng thuộc tính bounds của UIView nếu bạn muốn biết kích thước của một chế độ xem nhất định hoặc [[UIScreen mainScreen] bounds] nếu bạn muốn biết kích thước màn hình của thiết bị.

+0

Cảm ơn bạn đã trả lời – haisergeant

1

Tương tự như trên nhưng hơi ít tiết:

CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width; 
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height; 
8

sử dụng mã này để sửa chữa trong iOS8:

float SW; 
float SH; 
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
if (([[[UIDevice currentDevice] systemVersion] floatValue]<8) && UIInterfaceOrientationIsLandscape(orientation)) 
{ 
    SW = [[UIScreen mainScreen] bounds].size.height; 
    SH = [[UIScreen mainScreen] bounds].size.width; 
} 
else 
{ 
    SW = [[UIScreen mainScreen] bounds].size.width; 
    SH = [[UIScreen mainScreen] bounds].size.height; 
} 
+0

Trong trường hợp 'chiều cao' và' chiều rộng' đang được gán cho biến sai. –

+0

@MurraySagal đó là vấn đề, nó kiểm tra hướng và thiết lập chiều cao và chiều rộng khác nhau trên iOS 7 trở xuống, khi nó ở chế độ ngang. – cortices

0

Sử dụng giới hạn của UIScreen là một cách hay để thực hiện nhưng bạn nên sử dụng các chức năng CGGeometry để truy cậpDữ liệu củathay vì truy cập trực tiếp. Xem CGGeometrydocs.

CGRect screenBound = [[UIScreen mainScreen] bounds]; 
CGFloat screenWidth = CGRectGetWidth(screenBound); 
CGFloat screenHeight = CGRectGetHeight(screenBound); 
1
float screen_Height; 
float screen_Width; 

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) { 
    screen_Height = [[UIScreen mainScreen] bounds].size.height; 
    screen_Width = [[UIScreen mainScreen] bounds].size.width; 
} else { 
    screen_Height = ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width); 
    screen_Width = ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height); 
} 
+0

Vâng, iOS 8 'cố định' kích thước màn hình chính, và bây giờ tất cả các ứng dụng cũ sử dụng 'hack', phải được cập nhật: ( – Coolant

0

Giả sử bạn muốn lấy định hướng hiện tại vào tài khoản, sử dụng:

float screen_width; 
float screen_height; 
float vers = [[[UIDevice currentDevice] systemVersion] floatValue]; 
UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation; 
if (vers < 8 && UIInterfaceOrientationIsLandscape(orient)) 
{ 
    screen_height = [[UIScreen mainScreen] bounds].size.width; 
    screen_width = [[UIScreen mainScreen] bounds].size.height; 
} 
else 
{ 
    screen_width = [[UIScreen mainScreen] bounds].size.width; 
    screen_height = [[UIScreen mainScreen] bounds].size.height; 
} 
13

Đây là một 'nhanh chóng' giải pháp: (Cập nhật cho 3.x nhanh chóng)

let screenWidth = UIScreen.main.fixedCoordinateSpace.bounds.width 
let screenHeight = UIScreen.main.fixedCoordinateSpace.bounds.height 

Báo cáo này ở các điểm không phải là pixel và "luôn luôn phản ánh [s] kích thước màn hình của thiết bị trong một bức chân dung ori entation "(Apple Docs). Không cần phải bận tâm với UIAnnoyinglyLongDeviceOrientation!

Nếu bạn muốn chiều rộng và chiều cao để phản ánh hướng thiết bị:

let screenWidth = UIScreen.main.bounds.width 
let screenHeight = UIScreen.main.bounds.height 

Đây chiều rộng và chiều cao sẽ lật giá trị flop tùy thuộc vào việc màn hình là trong bức chân dung hoặc phong cảnh.

Và đây là làm thế nào để có được kích thước màn hình được đo bằng pixel không điểm:

let screenWidthInPixels = UIScreen.main.nativeBounds.width 
let screenHeightInPixels = UIScreen.main.nativeBounds.height 

này cũng "được dựa trên thiết bị trong một hướng dọc-up.. Giá trị này không thay đổi khi thiết bị quay" (Apple Docs)

Xin lưu ý rằng cho 2.x nhanh chóng và thấp hơn, bạn nên sử dụng UIScreen.mainScreen() thay vì UIScreen.main

1

Dưới đây là một cách Swift để có được kích thước màn hình:

var screenWidth: CGFloat { 
    if UIInterfaceOrientationIsPortrait(screenOrientation) { 
     return UIScreen.mainScreen().bounds.size.width 
    } else { 
     return UIScreen.mainScreen().bounds.size.height 
    } 
} 
var screenHeight: CGFloat { 
    if UIInterfaceOrientationIsPortrait(screenOrientation) { 
     return UIScreen.mainScreen().bounds.size.height 
    } else { 
     return UIScreen.mainScreen().bounds.size.width 
    } 
} 
var screenOrientation: UIInterfaceOrientation { 
    return UIApplication.sharedApplication().statusBarOrientation 
} 

Đây là những bao gồm như là một chức năng tiêu chuẩn here.

Đây cũng là trong tài liệu.

Các vấn đề liên quan