Skip to content

Commit

Permalink
Merge pull request #1345 from onevcat/fix/unconstraint-size
Browse files Browse the repository at this point in the history
Fix unconstraint size
  • Loading branch information
onevcat committed Nov 12, 2019
2 parents 19c636d + 77bebfa commit fbef8d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Sources/Extensions/ImageView+Kingfisher.swift
Expand Up @@ -356,6 +356,17 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {
equalTo: base.centerXAnchor, constant: newIndicator.centerOffset.x).isActive = true
view.centerYAnchor.constraint(
equalTo: base.centerYAnchor, constant: newIndicator.centerOffset.y).isActive = true

switch newIndicator.sizeStrategy(in: base) {
case .intrinsicSize:
break
case .full:
view.heightAnchor.constraint(equalTo: base.heightAnchor, constant: 0).isActive = true
view.widthAnchor.constraint(equalTo: base.widthAnchor, constant: 0).isActive = true
case .size(let size):
view.heightAnchor.constraint(equalToConstant: size.height).isActive = true
view.widthAnchor.constraint(equalToConstant: size.width).isActive = true
}

newIndicator.view.isHidden = true
}
Expand Down
21 changes: 21 additions & 0 deletions Sources/Views/Indicator.swift
Expand Up @@ -67,13 +67,30 @@ public protocol Indicator {

/// The indicator view which would be added to the super view.
var view: IndicatorView { get }

/// The size strategy used when adding the indicator to image view.
/// - Parameter imageView: The super view of indicator.
func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy
}

public enum IndicatorSizeStrategy {
case intrinsicSize
case full
case size(CGSize)
}

extension Indicator {

/// Default implementation of `centerOffset` of `Indicator`. The default value is `.zero`, means that there is
/// no offset for the indicator view.
public var centerOffset: CGPoint { return .zero }


/// Default implementation of `centerOffset` of `Indicator`. The default value is `.full`, means that the indicator
/// will pin to the same height and width as the image view.
public func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy {
return .full
}
}

// Displays a NSProgressIndicator / UIActivityIndicatorView
Expand Down Expand Up @@ -114,6 +131,10 @@ final class ActivityIndicator: Indicator {
}
}

func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy {
return .intrinsicSize
}

init() {
#if os(macOS)
activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
Expand Down

0 comments on commit fbef8d9

Please sign in to comment.