2011-04-09 98 views
4

我用下面的代碼獲取屏幕尺寸寬度:的iPad - 獲取屏幕大小在縱向和橫向

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

但在肖像其給寬度爲「668.0」以及景觀。 如何根據設備的方向獲得不同的寬度。

回答

3

我遇到同樣的問題,因爲你和所有我能找到的是檢查的方向和計算的高度和寬度如下:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
screenHeight = orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight ? 748 : 1004; 
screenWidth = orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight ? 1024 : 768; 

這些大小對於iPad來說很明顯,但它可以在適當的尺寸下適用於iPhone。

我個人認爲這有點差,但我找不到其他東西。

+0

工作得很好,即使沒有「查看」,謝謝。 – 2011-12-13 05:46:23

+0

而不是 'orientation == UIInterfaceOrientationLandscapeLeft ||方向== UIInterfaceOrientationLandscapeRight' 或 '方向== UIInterfaceOrientationLandscapeLeft ||方向== UIInterfaceOrientationLandscapeRight' 你可以使用'UIInterfaceOrientationIsPortrait(orientation)'和'UIInterfaceOrientationIsLandscape(orientation)'。 – 2014-01-14 19:32:22

1
CGSize winSize = [[CCDirector sharedDirector] winSize];

winSize.width將給你寬度和winSize.height給你的身高。

+0

我沒有使用cocos2d。 iPhone原生SDK有沒有類似的方法(不是在cocos2d或其他)? – Satyam 2011-04-11 12:09:52

3

好的。在這種情況下,我們可以使用,

CGRect viewFrame = self.view.frame; 
int width = viewFrame.size.width; 
int height = viewFrame.size.height;
+0

我的類繼承自NSObject。沒有辦法使用視圖,所以我想用UIScreen。可能是我會嘗試別的東西。 – Satyam 2011-04-12 06:04:38

+0

好的。祝一切順利。 :) – ShinuShajahan 2011-04-12 07:10:56

相關問題