diff --git a/Masonry/View+MASAdditions.m b/Masonry/View+MASAdditions.m index b99316ad..48a5cb7e 100644 --- a/Masonry/View+MASAdditions.m +++ b/Masonry/View+MASAdditions.m @@ -184,23 +184,20 @@ - (void)setMas_key:(id)key { objc_setAssociatedObject(self, @selector(mas_key), key, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -#pragma mark - heirachy +#pragma mark - hierarchy - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view { - MAS_VIEW *closestCommonSuperview = nil; - + if (!view) { + return nil; + } + MAS_VIEW *firstViewSuperview = self; MAS_VIEW *secondViewSuperview = view; - while (!closestCommonSuperview && secondViewSuperview) { - MAS_VIEW *firstViewSuperview = self; - while (!closestCommonSuperview && firstViewSuperview) { - if (secondViewSuperview == firstViewSuperview) { - closestCommonSuperview = secondViewSuperview; - } - firstViewSuperview = firstViewSuperview.superview; - } - secondViewSuperview = secondViewSuperview.superview; + + while (firstViewSuperview != secondViewSuperview) { + firstViewSuperview = firstViewSuperview == nil ? view : firstViewSuperview.superview; + secondViewSuperview = secondViewSuperview == nil ? self : secondViewSuperview.superview; } - return closestCommonSuperview; + return firstViewSuperview; } @end