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

更新SDAutoLayout的Swift扩展,使用Swifty风格的点语法实现SDAutoLayout布局 #316

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

fanyuexiang
Copy link
Contributor

before

extension UIView {
    @discardableResult
    public func layout() -> SDAutoLayoutModel {
        return sd_layout()
    }
}

// example:not swifty and the function name 'layout()' is easy to repeat
yourView.layout()
   .topTo(view, 10)
   .leftTo(view, 10)
   .width(is: 100)
   .height(is: 100)

now

public final class SDAutoLayout<Base> {
    public let base: Base
    public init(_ base: Base) {
        self.base = base
    }
}

public protocol SDAutoLayoutCompatible {
    associatedtype CompatibleType
    var sd: CompatibleType { get }
}

public extension SDAutoLayoutCompatible {
    var sd: SDAutoLayout<Self> {
        get { return SDAutoLayout(self) }
    }
}

extension UIView: SDAutoLayoutCompatible { }

extension SDAutoLayout where Base: UIView {
    @discardableResult
    public func layout() -> SDAutoLayoutModel {
        return base.sd_layout();
    }
}

// example:  swifty 🎉🎉🎉
yourView.sd.layout()
       .topTo(view, 10)
       .leftTo(view, 10)
       .width(is: 100)
       .height(is: 100)

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

Successfully merging this pull request may close these issues.

1 participant