我已经使用此代码来获取屏幕宽度和屏幕高度,
float scaleFactor = [[UIScreen mainScreen] scale];
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat widthInPixel = screen.size.width * scaleFactor;
CGFloat heightInPixel = screen.size.height * scaleFactor;
NSLog(@"%f",widthInPixel);
NSLog(@"%f",heightInPixel);
和
CGRect screenBounds = [[UIScreen mainScreen] bounds];
NSLog(@"screenBounds W %f",screenBounds.size.width);
NSLog(@"screenBounds H %f",screenBounds.size.height);
但是对于纵向和横向模式,其显示的宽度均为768,高度为1024。
最佳答案
这将为您提供良好的解释-How to get orientation-dependent height and width of the screen?
和为建议的Here's a handy macro:定义宏的方法之一。
#define SCREEN_WIDTH(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication] .statusBarOrientation)?[[UIScreen mainScreen] bounds] .size.width:[[UIScreen mainScreen] bounds] .size.height)
#define SCREEN_HEIGHT(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication] .statusBarOrientation)?[[UIScreen mainScreen] bounds] .size.height:[[UIScreen mainScreen] bounds] .size.width)
关于ios - 在纵向和横向模式下获得相同的屏幕宽度和高度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17611266/