我有一个UIImageView
的网格。 5S的布局看起来很好,但是6或6+的网格位于左上角。我决定尝试测试屏幕大小,这在this问题中解释(测试工作);然后根据屏幕尺寸,按照this帖子中的建议,使用UIImageView
重新定位CGRectMake
。这是代码:
if UIDevice().userInterfaceIdiom == .Phone {
switch UIScreen.mainScreen().nativeBounds.height {
case 480:
println("iPhone Classic")
case 960:
println("iPhone 4 or 4S")
case 1136:
println("Default Setup")
println("iPhone 5 or 5S or 5C")
case 1334:
dieImage0.frame = CGRectMake(42, 61, 152, 152)
dieImage1.frame = CGRectMake(247, 61, 152, 152)
dieImage2.frame = CGRectMake(42, 218, 152, 152)
dieImage3.frame = CGRectMake(247, 218, 152, 152)
dieImage4.frame = CGRectMake(42, 376, 152, 152)
dieImage5.frame = CGRectMake(247, 376, 152, 152)
dieImage6.frame = CGRectMake(42, 533, 152, 152)
dieImage7.frame = CGRectMake(247, 533, 152, 152)
println("iPhone 6")
case 2208:
dieImage0.frame = CGRectMake(141, 201, 228, 228)
dieImage1.frame = CGRectMake(819, 201, 228, 228)
dieImage2.frame = CGRectMake(141, 723, 228, 228)
dieImage3.frame = CGRectMake(819, 723, 228, 228)
dieImage4.frame = CGRectMake(141, 1242, 228, 228)
dieImage5.frame = CGRectMake(819, 1242, 228, 228)
dieImage6.frame = CGRectMake(141, 1764, 228, 228)
dieImage7.frame = CGRectMake(819, 1764, 228, 228)
println("iPhone 6+")
default:
println("unknown")
}
}
我知道测试有效,因为正确打印手机型号的信息,但图像的位置不会改变。然后当我触发segue转到第二个视图控制器
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showMenu" {
println("You pressed the menu button")
}
错误"致命错误:在展开可选值时意外发现nil"被打印到控制台和案例中的第一行,以更改我正在测试的设备的图像位置抛出此错误:Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
。任何想法为什么会这样?
答案 0 :(得分:1)
试试这个,
1)检查imageview的插座连接
2)在viewWillAppear下添加此func,如下所示
override func viewWillAppear(animated: Bool) {
...
...
let screenSize: CGRect = UIScreen.mainScreen().bounds
screenSize.origin
let screenWidth = screenSize.width;
let screenHeight = screenSize.height;
if(screenHeight == 667)
{
//do Something
}
else if(screenHeight == 667)
{
//do Something
}
}