Skip to content

Instantly share code, notes, and snippets.

@hiXgb
Created November 27, 2016 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiXgb/6cf33f97fa2439a44d23be3e4691b16e to your computer and use it in GitHub Desktop.
Save hiXgb/6cf33f97fa2439a44d23be3e4691b16e to your computer and use it in GitHub Desktop.
- (UIView *) findCommonSuperViewBetween: (UIView *)viewA
and: (UIView *)viewB
{
if (!viewA && !viewB) {
return nil;
}
if (!viewA) {
return viewB.superview;
}
if (!viewB) {
return viewA.superview;
}
NSMutableArray *viewASuperViews = [NSMutableArray new];
NSMutableArray *viewBSuperViews = [NSMutableArray new];
UIView *superViewA = viewA.superview;
UIView *superViewB = viewB.superview;
BOOL getAllASuperViews = NO;
BOOL getAllBSuperViews = NO;
BOOL found = NO;
while (!found) {
if (!getAllASuperViews) {
if (superViewA) {
[viewASuperViews addObject:superViewA];
superViewA = superViewA.superview;
}
else
{
getAllASuperViews = YES;
}
}
if (!getAllBSuperViews) {
if (superViewB) {
[viewBSuperViews addObject:superViewB];
superViewB = superViewB.superview;
}
else
{
getAllBSuperViews = YES;
}
}
found = (getAllASuperViews && getAllBSuperViews);
}
UIView *commonSuperView = nil;
if (viewASuperViews.count < viewBSuperViews.count) {
commonSuperView = viewASuperViews.firstObject;
}
else
{
commonSuperView = viewBSuperViews.firstObject;
}
return commonSuperView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment