How can I do Key Value Observing and get a KVO callback on a UIView's frame? How can I do Key Value Observing and get a KVO callback on a UIView's frame? ios ios

How can I do Key Value Observing and get a KVO callback on a UIView's frame?


There are usually notifications or other observable events where KVO isn't supported. Even though the docs says 'no', it is ostensibly safe to observe the CALayer backing the UIView. Observing the CALayer works in practice because of its extensive use of KVO and proper accessors (instead of ivar manipulation). It's not guaranteed to work going forward.

Anyway, the view's frame is just the product of other properties. Therefore we need to observe those:

[self.view addObserver:self forKeyPath:@"frame" options:0 context:NULL];[self.view.layer addObserver:self forKeyPath:@"bounds" options:0 context:NULL];[self.view.layer addObserver:self forKeyPath:@"transform" options:0 context:NULL];[self.view.layer addObserver:self forKeyPath:@"position" options:0 context:NULL];[self.view.layer addObserver:self forKeyPath:@"zPosition" options:0 context:NULL];[self.view.layer addObserver:self forKeyPath:@"anchorPoint" options:0 context:NULL];[self.view.layer addObserver:self forKeyPath:@"anchorPointZ" options:0 context:NULL];[self.view.layer addObserver:self forKeyPath:@"frame" options:0 context:NULL];

See full example herehttps://gist.github.com/hfossli/7234623

NOTE: This is not said to be supported in the docs, but it works as of today with all iOS versions this far (currently iOS 2 -> iOS 11)

NOTE: Be aware that you will receive multiple callbacks before it settles at its final value. For example changing the frame of a view or layer will cause the layer to change position and bounds (in that order).


With ReactiveCocoa you can do

RACSignal *signal = [RACSignal merge:@[  RACObserve(view, frame),  RACObserve(view, layer.bounds),  RACObserve(view, layer.transform),  RACObserve(view, layer.position),  RACObserve(view, layer.zPosition),  RACObserve(view, layer.anchorPoint),  RACObserve(view, layer.anchorPointZ),  RACObserve(view, layer.frame),  ]];[signal subscribeNext:^(id x) {    NSLog(@"View probably changed its geometry");}];

And if you only want to know when bounds changes you can do

@weakify(view);RACSignal *boundsChanged = [[signal map:^id(id value) {    @strongify(view);    return [NSValue valueWithCGRect:view.bounds];}] distinctUntilChanged];[boundsChanged subscribeNext:^(id ignore) {    NSLog(@"View bounds changed its geometry");}];

And if you only want to know when frame changes you can do

@weakify(view);RACSignal *frameChanged = [[signal map:^id(id value) {    @strongify(view);    return [NSValue valueWithCGRect:view.frame];}] distinctUntilChanged];[frameChanged subscribeNext:^(id ignore) {    NSLog(@"View frame changed its geometry");}];


EDIT: I don't think this solution is thorough enough. This answer is kept for historical reasons. See my newest answer here: https://stackoverflow.com/a/19687115/202451


You've got to do KVO on the frame-property. "self" is in thise case a UIViewController.

adding the observer (typically done in viewDidLoad):

[self addObserver:self forKeyPath:@"view.frame" options:NSKeyValueObservingOptionOld context:NULL];

removing the observer (typically done in dealloc or viewDidDisappear:):

[self removeObserver:self forKeyPath:@"view.frame"];

Getting information about the change

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {    if([keyPath isEqualToString:@"view.frame"]) {        CGRect oldFrame = CGRectNull;        CGRect newFrame = CGRectNull;        if([change objectForKey:@"old"] != [NSNull null]) {            oldFrame = [[change objectForKey:@"old"] CGRectValue];        }        if([object valueForKeyPath:keyPath] != [NSNull null]) {            newFrame = [[object valueForKeyPath:keyPath] CGRectValue];        }    }} 


Currently it's not possible to use KVO to observe a view's frame. Properties have to be KVO compliant to be observable. Sadly, properties of the UIKit framework are generally not observable, as with any other system framework.

From the documentation:

Note: Although the classes of the UIKit framework generally do not support KVO, you can still implement it in the custom objects of your application, including custom views.

There are a few exceptions to this rule, like NSOperationQueue's operations property but they have to be explicitly documented.

Even if using KVO on a view's properties might currently work I would not recommend to use it in shipping code. It's a fragile approach and relies on undocumented behavior.