Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why SnapKit has no closestCommonSuperview function? #739

Open
Chaselop opened this issue Jan 28, 2022 · 1 comment
Open

Why SnapKit has no closestCommonSuperview function? #739

Chaselop opened this issue Jan 28, 2022 · 1 comment

Comments

@Chaselop
Copy link

Recently, I read the source code of Snapkit and Masonry, I find that Snapkit doesn't check two views's commonSuperview, but instead, Masonry has this func, why?

@check4ismail
Copy link

check4ismail commented Mar 21, 2022

Hey @Chaselop,

Not sure why SnapKit doesn't have the closestCommonSuperview function - but nonetheless you can always build your own solution. Here's a closestCommonSuperview function I created without using any 3rd party libraries. Hope that helps!

func fetchClosestCommonSuperView(_ lhs: UIView, _ rhs: UIView) -> UIView? {
        var leftSuperViews: [UIView] = []
        var currentSuperView = lhs.superview
        while let superView = currentSuperView {
            leftSuperViews.append(superView)
            currentSuperView = currentSuperView?.superview
        }
        
        currentSuperView = rhs.superview
        while let superView = currentSuperView {
            if leftSuperViews.contains(superView) {
                return superView
            }
            
            currentSuperView = currentSuperView?.superview
        }
        
        return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants