Merge pull request #17 from SDWebImage/bugfix_view_wrapper_mac

Using the frame layout for view wrapper instead of Autolayout. Solve the issue when running on AppKit (not Catalyst)
This commit is contained in:
DreamPiggy 2019-10-05 19:06:05 +08:00 committed by GitHub
commit 9ab29c1b0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 20 deletions

View File

@ -31,34 +31,26 @@ public class AnimatedImageViewWrapper : PlatformView {
ctx.setShouldAntialias(shouldAntialias)
}
#if os(macOS)
public override func layout() {
super.layout()
wrapped.frame = self.bounds
}
#else
public override func layoutSubviews() {
super.layoutSubviews()
wrapped.frame = self.bounds
}
#endif
public override init(frame frameRect: CGRect) {
super.init(frame: frameRect)
addSubview(wrapped)
wrapped.bindFrameToSuperviewBounds()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
addSubview(wrapped)
wrapped.bindFrameToSuperviewBounds()
}
}
extension PlatformView {
/// Adds constraints to this `UIView` instances `superview` object to make sure this always has the same size as the superview.
/// Please note that this has no effect if its `superview` is `nil` add this `UIView` instance as a subview before calling this.
func bindFrameToSuperviewBounds() {
guard let superview = self.superview else {
print("Error! `superview` was nil call `addSubview(view: UIView)` before calling `bindFrameToSuperviewBounds()` to fix this.")
return
}
self.translatesAutoresizingMaskIntoConstraints = false
self.topAnchor.constraint(equalTo: superview.topAnchor, constant: 0).isActive = true
self.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: 0).isActive = true
self.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: 0).isActive = true
self.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: 0).isActive = true
}
}