根据设备大小改变 UIImageViews 的位置 [英] Changing the position of UIImageViews based on the size of the device
问题描述
我有一个 UIImageView
的网格.布局在 5S 上看起来不错,但在 6 或 6+ 上,网格位于左上角.我决定尝试测试屏幕尺寸,这在 this 问题(测试有效);然后根据屏幕尺寸,按照 this 帖子.代码如下:
I have a grid of UIImageView
s. The layout looks fine on the 5S but the on the 6 or 6+ the grid is in the upper left corner. I decided to try to test for the screen size, which is explained in this question (The test works); then based on the screen size, re-position the UIImageView
s using CGRectMake
as suggested in this post. Here is the code:
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 转到第二个视图控制器时
I know test works because the message saying the phone model is printed correctly, but the position of the images does not change. Then when I fire the segue to go to the second view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showMenu" {
println("You pressed the menu button")
}
错误致命错误:在解包可选值时意外发现 nil"被打印到控制台,并且在更改我正在测试的设备的图像位置的情况下的第一行抛出此错误:线程1:EXC_BAD_INSTRUCTION(代码=EXC_1386_INVOP,子代码=0x0)
.知道为什么会发生这种情况吗?
The error "fatal error: unexpectedly found nil while unwrapping an Optional value" gets printed to the console and the first line in the case to change the position of an image for the device I am testing on throws this error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
. Any ideas why this happens?
推荐答案
试试这个,
1) 检查 imageview 的插座连接
1) Check outlet connection of your imageview
2) 在 viewWillAppear 下添加这个函数,如下图
2) Add this func under viewWillAppear like below
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
}
}
这篇关于根据设备大小改变 UIImageViews 的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!