Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a UIView resize event?

Tags:

ios

iphone

ipad

I have a view that has rows and columns of imageviews in it.

If this view is resized, I need to rearrange the imageviews positions.

This view is a subview of another view that gets resized.

Is there a way to detect when this view is being resized?

like image 225
live-love Avatar asked Oct 22 '10 20:10

live-love


2 Answers

As Uli commented below, the proper way to do it is override layoutSubviews and layout the imageViews there.

If, for some reason, you can't subclass and override layoutSubviews, observing bounds should work, even when being kind of dirty. Even worse, there is a risk with observing - Apple does not guarantee KVO works on UIKit classes. Read the discussion with Apple engineer here: When does an associated object get released?

original answer:

You can use key-value observing:

[yourView addObserver:self forKeyPath:@"bounds" options:0 context:nil]; 

and implement:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {     if (object == yourView && [keyPath isEqualToString:@"bounds"]) {         // do your stuff, or better schedule to run later using performSelector:withObject:afterDuration:     } } 
like image 92
Michal Avatar answered Sep 30 '22 14:09

Michal


In a UIView subclass, property observers can be used:

override var bounds: CGRect {     didSet {         // ...     } } 

Without subclassing, key-value observation with smart key-paths will do:

var boundsObservation: NSKeyValueObservation?  func beginObservingBounds() {     boundsObservation = observe(\.bounds) { capturedSelf, _ in         // ...     } } 
like image 41
Rudolf Adamkovič Avatar answered Sep 30 '22 14:09

Rudolf Adamkovič