Kingfisher: Option to set loading activity indicator color.

Created on 5 Mar 2016  路  3Comments  路  Source: onevcat/Kingfisher

As can be seen in the code below, the color of activity indicator is assigned based on the platform. For iOS this is .Gray. I'd really like to use the .WhiteLarge option as I'm working with a dark background color before the image loads. Unfortunately, this option cannot be set. This would be a nice feature to have.

        set {
            if kf_showIndicatorWhenLoading == newValue {
                return
            } else {
                if newValue {

#if os(OSX)
                    let indicator = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
                    indicator.controlSize = .SmallControlSize
                    indicator.style = .SpinningStyle
#else
    #if os(tvOS)
                    let indicatorStyle = UIActivityIndicatorViewStyle.White
    #else
                    let indicatorStyle = UIActivityIndicatorViewStyle.Gray
    #endif
                    let indicator = UIActivityIndicatorView(activityIndicatorStyle:indicatorStyle)
                    indicator.autoresizingMask = [.FlexibleLeftMargin, .FlexibleRightMargin, .FlexibleBottomMargin, .FlexibleTopMargin]

Most helpful comment

For the record, here's how you can set it with kf.indicator:

(imageView.kf.indicator?.view as? UIActivityIndicatorView)?.color = .white

I don't really like all those ? but I think that's the best way to do it.

Another solution would be to add an associated value to .activity but it would break existing projects.

public enum IndicatorType {
    /// No indicator.
    case none
    /// Use system activity indicator.
    case activity(let color: UIColor?) // Too bad we cannot set its default value to nil
    /// Use an image as indicator. GIF is supported.
    case image(imageData: Data)
    /// Use a custom indicator, which conforms to the `Indicator` protocol.
    case custom(indicator: Indicator)
}

Existing projects would have to change .kf.indicatorType = .activity to .kf.indicatorType = .activity(color: nil).

All 3 comments

Hi, @kbpontius

I guess you could get the indicator view by accessing imageView.kf_indicator and then set it there. Does it work for you?

Good suggestion, I'll give it a shot. Thanks!

For the record, here's how you can set it with kf.indicator:

(imageView.kf.indicator?.view as? UIActivityIndicatorView)?.color = .white

I don't really like all those ? but I think that's the best way to do it.

Another solution would be to add an associated value to .activity but it would break existing projects.

public enum IndicatorType {
    /// No indicator.
    case none
    /// Use system activity indicator.
    case activity(let color: UIColor?) // Too bad we cannot set its default value to nil
    /// Use an image as indicator. GIF is supported.
    case image(imageData: Data)
    /// Use a custom indicator, which conforms to the `Indicator` protocol.
    case custom(indicator: Indicator)
}

Existing projects would have to change .kf.indicatorType = .activity to .kf.indicatorType = .activity(color: nil).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HuseyinVural picture HuseyinVural  路  4Comments

joebenton picture joebenton  路  4Comments

vCrespoP picture vCrespoP  路  3Comments

jacowu picture jacowu  路  4Comments

Tarpsvo picture Tarpsvo  路  5Comments