Posts in ios7

iOS7 Subviews Hierarchy

In iOS6 a common approach to tweak the appearance of the UIView subclasses (like UISearchBar, UITextField etc.) was to cycle the subviews searching for a given view to alter.

1
2
3
4
5
6
7
8
9
10
11
id viewThatIWantToTweak = nil;
for (UIView *view in self.searchBar.subviews) {
    if ([view isKindOfClass:NSClassFromString(@“UISearchBarTextField”)]) {
        viewThatIWantToTweak = view;
        break;
    }
}</p>

<p>if (viewThatIWantToTweak) {
    // do something with viewThatIWantToTweak
}

The subviews property is defined in the in the UIView interface.

1
@property (nonatomic, readonly, copy) NSArray *subviews;

Unfortunately in iOS7 viewThatIWantToTweak in the previous example will always be nil. The view hierarchy has changed for (apparently) the majority of the UI elements. It is no more sufficient to cycle the subviews to effectively retrieve the desired view: it is necessary to search recursively the subviews array.

written in ios, ios7, objective-c Read on →