Changing the position of UIImageViews based on the

2019-08-31 11:12发布

I have a grid of UIImageViews. 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 UIImageViews 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")
                }
            }

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")
        }

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条回答
做自己的国王
2楼-- · 2019-08-31 12:14

Try this,

1) Check outlet connection of your imageview

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
  }


}
查看更多
登录 后发表回答