2010-09-07 96 views
73

嗨我想知道是否有一種方法來獲得寬度編程。iPhone/iPad:如何以編程方式獲取屏幕寬度?

我在尋找一些足以容納iphone 3gs,iphone 4,ipad的東西。此外,寬度應根據設備是縱向還是橫向(對於iPad)而改變。

任何人都知道如何做到這一點?我一直在尋找一段時間......謝謝!

+0

是屏幕的寬度,可用應用程序空間的寬度(沒有系統欄)或您正在尋找的特定視圖的寬度? – VdesmedT 2010-09-07 08:06:40

+0

hrmm我不確定。我想我只是想要設備的寬度(即在縱向寬度爲1024,而在橫向寬度爲1024),但self.view.bounds似乎滿足這一點。看到我下面的評論 – foreyez 2010-09-07 17:41:11

+0

請參閱[這個更全面的答案](http://stackoverflow.com/a/7905540/17339),考慮到設備的方向。 – 2012-04-19 22:52:09

回答

179

看看UIScreen

例如。

CGFloat width = [UIScreen mainScreen].bounds.size.width; 

如果您不想包含狀態欄(不會影響寬度),請查看applicationFrame屬性。

UPDATE:事實證明,UIScreen(-bounds或-applicationFrame)沒有考慮當前的界面方向。更正確的方法是詢問你的UIView的邊界 - 假設這個UIView已經被它的View控制器自動旋轉了。

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    CGFloat width = CGRectGetWidth(self.view.bounds); 
} 

如果視圖沒有被視圖控制器自動旋轉,那麼你將需要檢查的接口方向以確定視圖邊界的哪一部分表示「寬度」和「高度」。請注意,frame屬性將爲您提供UIWindow座標空間中視圖的矩形,該座標空間默認情況下不會考慮界面方向。

+3

實際上,當ipad面向橫向時,這不起作用。即,如果我的模擬器正在橫向運行,那麼它仍然返回768(而不是1024)。你認爲我應該有一個if語句來檢查方向嗎?還是有更好的方法來獲得寬度? – foreyez 2010-09-07 06:22:30

+0

該死的 - 看起來你是對的;我用更多的想法更新了我的答案。 – 2010-09-07 07:17:09

+0

我結束了剛剛使用「CGFloat寬度= CGRectGetWidth(self.view.bounds);」在我的方法(不需要didRotateFromInterfaceOrientation)...所以atleast self.view.bounds考慮到方向。謝謝!! – foreyez 2010-09-07 17:36:17

9
CGRect screen = [[UIScreen mainScreen] bounds]; 
CGFloat width = CGRectGetWidth(screen); 
//Bonus height. 
CGFloat height = CGRectGetHeight(screen); 
+10

沒有考慮到方向 – rimsky 2012-01-30 20:01:16

5

這可以在3行代碼完成:

// grab the window frame and adjust it for orientation 
UIView *rootView = [[[UIApplication sharedApplication] keyWindow] 
            rootViewController].view; 
CGRect originalFrame = [[UIScreen mainScreen] bounds]; 
CGRect adjustedFrame = [rootView convertRect:originalFrame fromView:nil]; 
+3

這很接近,但是如果顯示的是UIAlertView,它將是關鍵窗口,它的rootViewController屬性將爲零 – 2013-04-02 21:04:45

+0

@mattbezark真。我認爲這是一個角落案件。有一種替代方法可以獲得根視圖 - 並不像這個那樣乾淨,但是我會在任何人感興趣的時候發佈替代方案。 – memmons 2013-04-03 14:39:37

+0

謝謝。這項工作在iOS 7和8上。 – 2014-10-21 20:43:08

-2

這裏是一個斯威夫特的方式來獲得的屏幕尺寸,這也需要當前界面的方向考慮:

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 
} 

這些作爲標準功能包含在:

https://github.com/goktugyil/EZSwiftExtensions

0

從iOS 9.0開始,無法可靠地獲取方向。這是我使用的應用程序,我設計了只肖像模式的代碼,因此,如果應用程序是在橫向模式下打開它仍然將是準確的:

screenHeight = [[UIScreen mainScreen] bounds].size.height; 
screenWidth = [[UIScreen mainScreen] bounds].size.width; 
if (screenWidth > screenHeight) { 
    float tempHeight = screenWidth; 
    screenWidth = screenHeight; 
    screenHeight = tempHeight; 
} 
0

使用此代碼,這將有助於

[[UIScreen mainScreen] bounds].size.height 
[[UIScreen mainScreen] bounds].size.width 
0

使用:

NSLog(@"%f",[[UIScreen mainScreen] bounds].size.width) ;