【问题标题】:nativeScale -> crash on 3.5 inch iPhone 4SnativeScale -> 在 3.5 英寸 iPhone 4S 上崩溃
【发布时间】:2014-12-13 16:51:17
【问题描述】:

我有一个为 3.5 和 4 英寸设计的应用。我用以下方法调整了屏幕上的项目:

CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
    //4 inch code

} else {
    //3.5 inch code

}

然后 iPhone 6 和 6 plus 发布了,所以我也不得不调整它们的屏幕尺寸。幸运的是,iPhone 6 具有相同的规模,因此使用相同的方法。但到了 iPhone 6+ 时,它就不再工作了,因为它是 3 倍规模。

现在这就是为什么我将之前使用的方法更改为:

CGRect screenBounds = [[UIScreen mainScreen] bounds];
if ([UIScreen mainScreen].nativeScale > 2.1) {
    //6 plus

} else if (screenBounds.size.height == 568) {
    //4 inch / iPhone 6 code

} else {
//3.5 inch code

}

现在它可以在除 iPhone 4S 之外的所有设备上完美运行。它崩溃了。日志说:

2014-10-17 21:06:31.370 Lichtpunkt[1288:60b] -[UIScreen nativeScale]: unrecognized selector sent to instance 0x17539920 2014-10-17 21:06:31.372 Lichtpunkt[1288:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIScreen nativeScale]: unrecognized selector sent to instance 0x17539920' *** First throw call stack: (0x30b94fd3 0x3b40dccf 0x30b98967 0x30b97253 0x30ae67b8 0x4102b 0x3342fda5 0x3305d2c1 0x33046e3f 0x33121d6d 0x330466f3 0x3304639b 0x3302a03d 0x33029cd5 0x330296df 0x330294ef 0x3302321d 0x30b602a5 0x30b5dc49 0x30b5df8b 0x30ac8f0f 0x30ac8cf3 0x35a21663 0x3341416d 0x18d6b91 0x2c9d1 0x3b91aab7) libc++abi.dylib: terminating with uncaught exception of type NSException

问题是我无法将更改恢复到第一个解决方案,因为 iPhone 6+ 没有完全调整,我试过了。

谁能帮帮我?

【问题讨论】:

  • 如果你能用 AutoLayout 处理所有这些不是很好吗?
  • @MichaelDautermann 肯定会,但它不起作用,因为我有一个滚动视图,里面有一个 imageView,并且 zoomFactors 不适合 autoLayout

标签: ios objective-c iphone xcode adjustment


【解决方案1】:

[UIScreen nativeScale] 仅在 iOS 8 下可用。

这就是它在 iOS 7 和更旧的设备上崩溃的原因。

您需要想出一个适用于 iOS 8 和您想要支持的旧 iOS 版本的替代方案。 UIScreen 中还有一些其他的屏幕尺寸获取方法可以一直使用到 iOS 2,但是您需要考虑到当设备在纵向和横向模式之间移动时会交换高度和宽度。

【讨论】:

  • 哦,谢谢。这解释了为什么它可以在模拟器中工作,但不能在我的 iPhone 4S 7.1.1 上工作。你知道一个还是我应该同时实现,对于每种大小的一种方法(首先兼容 ios7 的方法)还是它仍然会崩溃?
  • 好吧,我的应用只支持纵向模式,所以不会有问题。非常感谢!
猜你喜欢
  • 2015-05-03
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多