0
votes

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 ?

2
[view addSubview: UIImageView] Did you do that? or added imageview to mainView?Mani
Show the code for the configuration of the views.Wain
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 ?Livevlad
@Livevlad autoresizing is working with storyboard too. You should have done something wrong like : your _image is nil or you have conflicting parameters between IB and your code. Or AutoLayout is enabled by default on IB so frames doesn't work.Tancrede Chazallet
@Livevlad I hope in the file inspector "Use Autolayout" is turned off? I know this is obvious, but otherwise why you are adding the autoresizing masks in the code? You can do that also in the storyboard.Anindya Sengupta

2 Answers

0
votes

try this:

_image.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; 
0
votes

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;