Search code examples
iosobjective-cuiviewsubviewkey-value-observing

Observe changes in a UIView's subviews array with KVO


I have looked around all over for an answer to this and I have tried to implement it, but nothing is working. Basically, I need to be able to observe changes in a VC view's subviews array. If an existing view is removed from that array, I want to be notified about it and run some code.

Is it possible?

EDIT - More information

I am trying to make a fix for a strange edge case bug where rapidly tapping on the UISearchBar of a UISearchDisplayController (very custom) causes the sdController (or rather the managed searchBar in navBar effect) to disappear from the view, but the sdController is STILL ACTIVE. Which means the navBar stays at the -y origin, and the tableView below isn't scrollable.

My original thought was to get to a state where the sdController was active, but the UISearchDisplayControllerContainerView wasn't in the view hierarchy. I tried testing this in the VC's viewDidLayoutSubviews, but alas, when you tap on a search bar and initiate the sdController animation, the sdController is active, and the UISearchDisplayControllerContainerView isn't in the view hierarchy :(.


Solution

  • As with most properties in Apple's frameworks subviews is not KVO compliant.

    If you control either the subview or the superview you can observe changes to the view hierarchy by subclassing and overriding:

    In the superview you have to override...

    - (void)willRemoveSubview:(UIView *)subview
    

    ... or, if you control the subview, you would override...

    - (void)willMoveToSuperview
    

    Both methods are called before the view is removed.