計算するためにiPhoneの画面サイズを取得するにはどうすればよいですか?
12 に答える
bounds
UIScreenのインスタンスでプロパティを使用できます。
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
ほとんどの人はメイン画面が欲しいでしょう。テレビにデバイスが接続されている場合は、代わりにUIScreenを繰り返し処理して、それぞれの境界を取得することをお勧めします。
詳細については、UIScreenクラスのドキュメントをご覧ください。
これが「swift」ソリューションです:(swift3.x用に更新)
let screenWidth = UIScreen.main.fixedCoordinateSpace.bounds.width
let screenHeight = UIScreen.main.fixedCoordinateSpace.bounds.height
これは、ピクセルではなくポイントでレポートし、「デバイスの画面の寸法を常に縦向きに反映します」(Apple Docs)。UIAnnoyinglyLongDeviceOrientationを気にする必要はありません!
幅と高さをデバイスの向きに反映させたい場合:
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
ここで、幅と高さは、画面が縦向きか横向きかに応じてフリップフロップの値を反転します。
そして、これがポイントではなくピクセルで測定された画面サイズを取得する方法です:
let screenWidthInPixels = UIScreen.main.nativeBounds.width
let screenHeightInPixels = UIScreen.main.nativeBounds.height
これも「縦向きのデバイスに基づいています。この値は、デバイスが回転しても変化しません」。(Apple Docs)
UIScreen.mainScreen()
swift 2.x以下の場合は、代わりにを使用する必要があることに注意してくださいUIScreen.main
このコードを使用して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;
}
特定のビューのサイズを知りたい場合、またはデバイスの画面サイズを知りたい場合は、のプロパティbounds
を使用します。UIView
[[UIScreen mainScreen] bounds]
上記と同様ですが、少し冗長ではありません。
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
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);
}
画面サイズを取得するSwiftの方法は次のとおりです。
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
}
これらは標準機能としてここに含まれています。
これはドキュメントにもあります。
[[UIScreen mainScreen] bounds]は、デバイスの向きを考慮せずに物理的な画面サイズを返します。
現在の向きに応じて画面サイズを取得する必要がある場合は、画面の向きに依存する高さと幅を取得する方法をご覧ください。
'の境界を使用するUIScreen
ことはそれを行うための良い方法ですが、データに直接アクセスするのではなく、'のデータCGGeometry
にアクセスするための関数を使用する必要があります。ドキュメントCGRect
を参照してください。CGGeometry
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);
現在の向きを考慮に入れたいと仮定して、以下を使用します。
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;
}
仲間のXamarianの場合-Xamarin.iOS+C#で画面サイズを取得する方法は次のとおりです。
var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;
var PT: CGFloat = 0;
override func viewDidLoad() {
super.viewDidLoad()
setPoints();
}
func setPoints() {
let screenBounds = UIScreen.main.bounds
let screenScale = UIScreen.main.scale
let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)
let nativeBounds = UIScreen.main.nativeBounds
let nativeScale = UIScreen.main.nativeScale
let pxScreenWidth:CGFloat = nativeBounds.width;
let ptScreenWidth:CGFloat = screenBounds.width;
PT = pxScreenWidth/ptScreenWidth/nativeScale;
}
使い方:
let label: UILabel = UILabel(frame: CGRect(x: 15 * PT, y: 10 * PT, width: self.view.frame.width - 30 * PT, height: self.view.frame.height - 20 * PT))