0

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?

Community
  • 1
  • 1
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
  • (1) You might want to consider `UICollectionView` or auto-layout constraints as a simpler, more elegant solution to this problem. (2) Do you know what "unexpectedly found nil while unwrapping an Optional value" means? Have you read related questions on this topic? – Aaron Brager Sep 07 '15 at 18:22
  • Where in the code you execute it? Furthermore, this is not a good solution to make such a grid. Use auto-layout and UICollectionView as @AaronBrager said. – Maysam Sep 07 '15 at 18:29
  • UICollectionView all the way. This is very bad code what you did there. – trusk Sep 07 '15 at 18:36

1 Answers1

1

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
  }


}
Manikandan D
  • 1,422
  • 1
  • 13
  • 25