UIView not autoresizing subview

2019-05-18 16:47发布

问题:

I have a UIView a that has autoresizing subviews enabled. It's size is:

 <UIView: 0x7650180; frame = (0 0; 320 394); clipsToBounds = YES; autoresize = LM+RM+H; layer = <CALayer: 0x7650230>>

I have another UIView b which i added to the view a. View b has flexible width/height. When I add it in, I expect it's height to change to 394, but it remains the same as the in the nib (420):

<UIView: 0x764f720; frame = (0 0; 320 460); autoresize = LM+W+RM+TM+H+BM; layer = <CALayer: 0x764f320>>

What am I doing wrong?

Thanks

回答1:

Autoresizing of subviews works only for subviews already added to a parent view. What should you do:

  1. Create a parent view.
  2. Add subviews.
  3. Set autoresizing masks for them.
  4. Resize the parent view.

Only then you will see that subviews are being autoresizes.

Adding subviews after resize will give no effect.



回答2:

Autoresize only affects subviews when the frame of the parent view changes. If you want your subview to have the same height as the parent view, you'll have to set the height to be equal, either before or after you add it as a subview.