我读取手机的本机高度来调整显示输出的大小,并确定顶部和底部的遮挡区域。这是一个基于标签的应用程序。我的显示器是纯编程的,它是图形布局的基础上,仅基于屏幕尺寸。在iPhone 12迷你,显示比它应该是小。其他iPhone 12类型显示正确。
当我在xcode模拟器中的各种iPhone类型上测试我的新应用程序时,它们看起来是正确的。但当我把应用程序放在我的iPhone 12迷你,显示比它应该是小。我了解到模拟器返回本地高度的值2436,但手机返回值2340。这段代码检查显示,并告诉我按手机类型在屏幕顶部和底部保留多少:
nativeHeight=UIScreen.main.nativeBounds.height
if nativeHeight==1136 || nativeHeight==1334 // SE,6S,7,8
{reserveTopPixels=40; reserveBottomPixels=98}
else if nativeHeight==1792 // 11, XR
{reserveTopPixels=88; reserveBottomPixels=198}
else if nativeHeight==2208 // 6s+ 7+ 8+
{reserveTopPixels=54; reserveBottomPixels=146}
else if nativeHeight==2436 || nativeHeight==2688 // X, XS, 11Pro, 11, Xr
{reserveTopPixels=132; reserveBottomPixels=260}
else if nativeHeight==2340 //iPhone 12 mini "downsampled screen"
{reserveTopPixels=132; reserveBottomPixels=260; nativeHeight = 2436}
else if nativeHeight==2532 // 12, 12 pro
{reserveTopPixels=132; reserveBottomPixels=260}
else if nativeHeight==2778 // 12 pro max
{reserveTopPixels=132; reserveBottomPixels=260}
else {reserveTopPixels=40; reserveBottomPixels=98}我的更正是将从电话2340收到的值更改为更大的值2436。
该应用程序现在正确地显示在模拟器和手机上。我的问题是,这是一个合理的解决办法,还是我应该尝试其他的方法。
发布于 2020-12-29 22:55:33
nativeBounds: CGRect.提供物理屏幕的边框,以像素为单位。iPhone 12 mini的物理屏幕为1080×2340像素。
iPhone 12 mini的物理屏幕分辨率为1080x2340像素(476像素)。这低于操作系统报告的本机分辨率(在UIScreen上使用UIScreen)。我认为这是为了与5.8“型号(iPhone X/XS/11 Pro)的分辨率保持兼容性。
参考资料:
这取决于您在何处使用此nativeHeight进行布局。
发布于 2021-01-02 12:34:05
正如您所指出的,您只是在iPhone 12迷你模拟器上遇到了问题。那你为什么需要所有的检查逻辑?只需确保您的工作模拟器和抓住iPhone 12迷你,并作出您的调整。我认为延长期限是有道理的。
static var isSimulator: Bool {
return TARGET_OS_SIMULATOR != 0
}
extension UIDevice {
var modelIdentifier = ""
modelIdentifier = ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"] ?? ""
let nativeHeight = UIScreen.main.nativeBounds.height
if nativeHeight==2340 // your specific case
{
reserveTopPixels=132; reserveBottomPixels=260; nativeHeight = 2436
}
}编辑
然后,如果希望某些代码仅在iOS模拟器中运行,则应使用以下方法:
func adjust() {
#if targetEnvironment(simulator)
// we're on the simulator
}
#else
// we're on a device
}
#endif
}https://stackoverflow.com/questions/65427814
复制相似问题