Question

I am facing a problem I really do not understand.

I have a view (grey rectangle) and a subview (red rectangle). The architecture is UIViewController => View(grey rectangle) => UIImageView (red rectangle).

I want the red rectangle to resize when the grey rectangle bounds change.

So I put the property "Autoresize subviews" to Yes for the grey rectangle and add the following code :

_image.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ;

But when I resize the grey rectangle, nothing happens for the red one : the grey one is bigger and the red one keeps the same frame.

Do you have any idea to help me ?

Update :

I added the imageView in the storyboard as subview of the "grey rectangle". I tried to add another UIImageView by code and that works.

So my new question is do you know why autoresizing is not working with subviews edited in storyboard ?

Was it helpful?

Solution

If you are willing to use Auto layout, your problems can be solved using the following steps:

  1. In the Storyboard, select the view - go to "file inspector" and check the box named "Use Autolayout"
  2. Add the following constraints:
    • Top space to container
    • Bottom space to container
    • Leading space to container and
    • Trailing space to container

This will -

  1. Just work
  2. Will be sustainable even when Autoresizing will no longer be used.

Edit


Check if the auto layout is turned off. If it is not, and you are trying to make the autoresize work through code, you will need to do the following for all the views you want to work with your autoresizing masks:

self.view.translatesAutoresizingMaskIntoConstraints = YES;

OTHER TIPS

try this:

_image.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top