Coolboylp
Coolboylp

Reputation: 53

Xcode 7 Swift 2 Object size different at run time

can someone please help me with my problem.

I have a Viewcontroller. I just added a "View" object and set the width of the "View" object to screen width which currently shows at 320 (iPhone 4s) at run time and design size. However when I check for the width of the "View" object at run time its showing me 240. The width property of the "View" object in design mode is showing me 320. I want my "View" object to adjust automatically to screen width depending on the iPhone used. For example if I simulate this on a iPhone 6, the screen width is 375 but the "View" is still showing me 240. The same goes for the height.

The same for the height as well. In design mode, the height is showing me 284 but at run time, its showing me 128.

I have a button inside the "View" object with a size of 80 x 80, but at run time, its showing me 46 x 30.

Here are my "View" settings:

View Object Height Constraints

View Object Vertical Constraints

My code for getting the width of the "View" is below:

   @IBOutlet weak var gameSpace: UIView!
   var gsWidth: CGFloat = 0
   var gsHeight: CGFloat = 0

  let gsSize: CGRect = gameSpace.bounds
  gsWidth = gsSize.width 
  gsHeight = gsSize.height

For the button...

    @IBOutlet weak var tapBtn: UIButton!
    let buttonSize: CGRect = tapBtn.bounds
    tapBtnWidth = buttonSize.width
    tapBtnHeight = buttonSize.height

Your help / suggestion will be highly appreciated. Thank you in advance.

Upvotes: 0

Views: 335

Answers (1)

slxl
slxl

Reputation: 689

What actually might be there is that you assign dimension-related properties before Auto-Layout made it's job and then keep using a wrong values.

if you need to update some values which depends on Auto-Layout - its better to do that here:

override func viewDidLayoutSubviews() {
       // Update your values there
}

When Auto-Layout engine finish all his job. Also keep in mind that this function might be called more than once during view lifecycle.

Upvotes: 1

Related Questions