2

There are many geometric related properties on UIView.

  • frame
  • bounds
  • transform

Maybe more.

If I want to execute some behavior when the view is resized, what should I do?

I usually tried overriding -layoutSubviews method, but it also be called by non-resizing event. Even I override all of the properties, I still can't sure I have handled all of possibilities.

What's most stable and recommended way to handle resize event?

eonil
  • 83,476
  • 81
  • 317
  • 516

2 Answers2

3

In Swift, you can do:

override var bounds: CGRect {
    didSet {
        // Do stuff here
    }
}
Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
2

I usually don't override layoutSubviews for resize, exactly because of what you said. I write my own layoutSubviewz and call it whenever it's needed, e.g. I override setFrame like this:

-(void)setFrame:(CGRect)frame
{
  [super setFrame:frame];
  [self layoutSubviewz];
} 
Kai Huppmann
  • 10,705
  • 6
  • 47
  • 78
  • I used this in Xamarin: public override CGRect Frame { get { return base.Frame; } set { MyCustomMethod(value); base.Frame = value; } } – pauldendulk Feb 14 '17 at 19:38