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
  • 79,444
  • 75
  • 307
  • 502

2 Answers2

3

In Swift, you can do:

override var bounds: CGRect {
    didSet {
        // Do stuff here
    }
}
Rudolf Adamkovič
  • 30,008
  • 11
  • 100
  • 116
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,623
  • 5
  • 45
  • 76
  • 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