Skip to content

Instantly share code, notes, and snippets.

@xNekOIx
Last active November 24, 2017 13:19
Show Gist options
  • Save xNekOIx/f8f0ecd8482d68b90c9773459a8d493e to your computer and use it in GitHub Desktop.
Save xNekOIx/f8f0ecd8482d68b90c9773459a8d493e to your computer and use it in GitHub Desktop.
Recursive view search for condition
typedef BOOL(^SCConditionBlock)(UIView *subview);
UIView * SCFindSubviewForCondition(UIView *view, SCConditionBlock conditionBlock);
UIView * SCFindSubviewForCondition(UIView *view, SCConditionBlock conditionBlock) {
NSCParameterAssert(view != nil);
if (conditionBlock(view)) {
return view;
}
let subviews = [view subviews];
__block UIView *satisfiedView = nil;
[subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
satisfiedView = SCFindSubviewForCondition(obj, conditionBlock);
if (satisfiedView) {
*stop = YES;
}
}];
return satisfiedView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment