Skip to content

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