Search code examples
iosuiimageviewautolayoutuipageviewcontroller

Change Constraints constant on view load has no effect


I have UIPageViewController that contains simple UIViewControllers. The View of the VC contains an UIImageView (aspect fit) at the top and a UITextView at the bottom. The LayoutConstraints for the ImageView are:

  • Fixed Width: Which has an Outlet to the VCs-Class
  • Fixed Height: Which has an Outlet to the VCs-Class
  • Center X is equal to the superviews centerX
  • Top is equal to Superviews MarinTop (+8)

The LayoutConstraints for the ImageView are:

  • Top is equal to ImageView bottom
  • Bottom is equal to superviews MarginBottom (+8)
  • Width is equal superviews width (Multiplicator is 0.8)

This works as expected.

But when the VC is loaded and has the image to display, I want to change the size of the imageView. There is a minimum and a maximum size and if a image view is smaller or larger it needs to be resized. Otherwise the the imageview has to get the size of the image.

So I calculate the size for the actual image and set the constant for the two LayoutConstraints. The values are correct but the ImageView does not change.

I tried to set them in viewWillLayoutSubviews but no change.

What am I doing wrong?


Solution

  • Try update your views inside viewDidLayoutSubviews:

    - (void) viewDidLayoutSubviews {
     // setup your constraints here
    }
    

    Hope this helps.