Created
November 27, 2016 09:29
-
-
Save hiXgb/90d9d4f7a571f9a250f0899168068cdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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) { | |
for (UIView *tmp in viewASuperViews) { | |
if ([viewBSuperViews containsObject:tmp]) { | |
commonSuperView = tmp; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
for (UIView *tmp in viewBSuperViews) { | |
if ([viewASuperViews containsObject:tmp]) { | |
commonSuperView = tmp; | |
break; | |
} | |
} | |
} | |
return commonSuperView; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment