2

I have a Swift app which sets a UIImageView to the size of a portion of the screen (set with autoconstraints to the margins and controls above it), and I use the height and width values of the ImageView to create 3 additional UIImageViews programmatically and position them on top of the original image. I am constructing a podium and place three images on top of the podium in appropriate positions. I then calculate the height and width of each additional image by creating a constant: let conImageHeight = 0.4 * self.imgPodium.frame.height. However, I can't get a reliable height and width of the original image.

Initially I used self.imgPodium.bounds.height which worked great for the iPhone 6 simulator screen size, but when I tried running the app on an iPad simulator, it kept the iPhone size values. The image itself changed size but when I println() the height and width using bounds, I get the values of the iPhone image.

I then changed and used self.imgPodium.frame.height but this didn't change anything.

Is there a better way to get the height and width of a UIImageView (or any control for that matter) after it positions itself through autoconstraints?

Thanks.

Questions I've looked at already but didn't help: Change UIImageView size and position programmatically in Swift
how to check screen size of iphone 4 and iphone 5 programmatically in swift
UIImageView Bounds, Frame
Cocoa: What's the difference between the frame and the bounds?
How to resize an UIImageView (swift)

Community
  • 1
  • 1
Matt Kelly
  • 1,451
  • 2
  • 13
  • 30
  • where are you trying to access the self.imgPodium dimensions? Has the view loaded yet? Maybe those constraints have not yet been calculated? – BHendricks May 17 '15 at 08:31
  • @Bhendricks Yes it is called in `override func viewDidLoad()` – Matt Kelly May 17 '15 at 08:55
  • You might need to either `layoutSubviews` in `viewDidLoad` to get the fully laid out numbers, or move the code to `viewDidAppear` – BHendricks May 17 '15 at 09:01
  • @BHendricks ok I called the code in `viewDidAppear` and that seemed to work. Do you know why it wasn't working in `viewDidLoad`? – Matt Kelly May 17 '15 at 09:25

1 Answers1

5

In viewDidLoad the subviews have been loaded and initialized but not layed out yet. Therefore their frame property for example is not valid yet and may be the one that was set in Xcode when designing the UI in the first place - that however is not reliable. You should do custom layout in places where you can be sure the layout has already happened yet: e.g in viewWillLayoutSubviews or viewDidLayoutSubviews. Both methods will also get called when the layout somehow needs to change. In viewDidAppear the layout has been done yet and therefore you can do some layouting too. This code however will get not get called when the layout changes due to interface rotation for example. AND it will get called after a presented viewController gets dismissed again. (not what you would want).

In both cases: do your setup of creating new imageViews in viewDidLoad and do the layout in viewWillLayoutSubviews or viewDidLayoutSubviews!

luk2302
  • 55,258
  • 23
  • 97
  • 137