Can I change the size of UIActivityIndicator?
Asked Answered
M

10

109

Whatever size i give to it while allocation, it shows fixed size only. Is it possible to increase it?

Code:

activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:
                     CGRectMake(142.00, 212.00, 80.0, 80.0)];
[[self view] addSubview:activityIndicator];
[activityIndicator sizeToFit];
activityIndicator.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
                                      UIViewAutoresizingFlexibleRightMargin |
                                      UIViewAutoresizingFlexibleTopMargin |
                                      UIViewAutoresizingFlexibleBottomMargin);
activityIndicator.hidesWhenStopped = YES;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
Morbific answered 14/4, 2010 at 14:20 Comment(1)
Check This #8586215Chopfallen
D
54

The size is fixed by the style. It's a standardized interface element so the API doesn't like to fiddle with it.

However, you probably could do a scaling transform on it. Not sure how that would affect it visually, however.

Just from a UI design perspective, its usually better to leave these common standardized elements alone. User have been taught that certain elements appear in a certain size and that they mean specific things. Altering the standard appearance alters the interface grammar and confuses the user.

Docilu answered 14/4, 2010 at 14:33 Comment(4)
That's great in certain contexts. However, what if this is in a splash screen, and all you see is the logo and maybe this teensy little spinner in the middle of the screen, it looks a bit silly. I can't believe Apple thinks one-size-fits-all is even a concept in UI elements.Diligent
@BenLeggiero You're not talking about UIActivityIndicatorViewStyleWhiteLarge, but about the small size instead, right? Because I think that this Large version has a nice size even on an empty screen.Hegemony
.whiteLarge does it! thx! I also set UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) and activityIndicator.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.38). On 6+/7+ screens the default one is just too tinyTidemark
It will look blurry if you do a scaling transform to increase the size.Gudrunguelderrose
S
171

The following will create an activity indicator 15px wide:

#import <QuartzCore/QuartzCore.h>

...

UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
activityIndicator.transform = CGAffineTransformMakeScale(0.75, 0.75);
[self addSubview:activityIndicator];

While I understand the sentiment of TechZen's answer, I don't think adjusting the size of a UIActivityIndicator by a relatively small amount is really a violation of Apple's standardized interface idioms - whether an activity indicator is 20px or 15px won't change a user's interpretation of what's going on.

Spikelet answered 1/3, 2012 at 20:18 Comment(2)
and the activity indicators are much too small on the iPad with having one in the middle of a webView...Priest
This seems to scale up the pixels, so not acceptable for large amounts, unfortunately.Auliffe
I
93

Swift 3.0 & Swift 4.0

self.activityIndi.transform = CGAffineTransform(scaleX: 3, y: 3)
Ion answered 19/6, 2017 at 10:56 Comment(4)
Exactly what I needed. This should be the top answer.Scratches
I love the one line solutions for simple matters! Now the only issue is how to anti alias it. At twice the size it looks a bit rough on Apple TVHuoh
@MicheleDall'Agata still there is no any native solution for the good quality graphics progress bar in iOS if you want to make custom than it is good but i suggest to use native bcoz of app size and app performanceIon
@HarshilKotecha Actually I have found out later that (at least for tvOS) in the IB there is a style for the activity gear that's called "Large White". That one has twice the proportions of the normal one, which is what I aimed to. The regulars are too small, good maybe for a single cell.Huoh
D
54

The size is fixed by the style. It's a standardized interface element so the API doesn't like to fiddle with it.

However, you probably could do a scaling transform on it. Not sure how that would affect it visually, however.

Just from a UI design perspective, its usually better to leave these common standardized elements alone. User have been taught that certain elements appear in a certain size and that they mean specific things. Altering the standard appearance alters the interface grammar and confuses the user.

Docilu answered 14/4, 2010 at 14:33 Comment(4)
That's great in certain contexts. However, what if this is in a splash screen, and all you see is the logo and maybe this teensy little spinner in the middle of the screen, it looks a bit silly. I can't believe Apple thinks one-size-fits-all is even a concept in UI elements.Diligent
@BenLeggiero You're not talking about UIActivityIndicatorViewStyleWhiteLarge, but about the small size instead, right? Because I think that this Large version has a nice size even on an empty screen.Hegemony
.whiteLarge does it! thx! I also set UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) and activityIndicator.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.38). On 6+/7+ screens the default one is just too tinyTidemark
It will look blurry if you do a scaling transform to increase the size.Gudrunguelderrose
B
47

It is possible to resize UIActivityIndicator.

CGAffineTransform transform = CGAffineTransformMakeScale(1.5f, 1.5f);
activityIndicator.transform = transform;

Original size is 1.0f. Now you increase and reduce size accordingly.

Bolter answered 27/10, 2015 at 8:39 Comment(1)
let transform = CGAffineTransformMakeScale(1.5, 1.5) activityIndicator.transform = transformAnnunciator
S
14

Swift3

var activityIndicator = UIActivityIndicatorView()
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
let transform: CGAffineTransform = CGAffineTransform(scaleX: 1.5, y: 1.5)
activityIndicator.transform = transform
activityIndicator.center = self.view.center
activityIndicator.startAnimating()
self.view.addSubview(activityIndicator)
Sufism answered 12/12, 2016 at 5:0 Comment(0)
O
5

Here is an extension that would work with Swift 3.0 & checks to prevent 0 scaling (or whatever value you want to prohibit):

extension UIActivityIndicatorView {
    func scale(factor: CGFloat) {
        guard factor > 0.0 else { return }

        transform = CGAffineTransform(scaleX: factor, y: factor)
    }
}

Call it like so to scale to 40 pts (2x):

activityIndicatorView.scale(factor: 2.0)
Ordway answered 21/3, 2017 at 20:34 Comment(0)
M
2

There also are lots of other useful "CGAffineTransform" tricks you can play with. For more details please see Apple Developer Library reference:

http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html

Good luck!

Mitchmitchael answered 22/5, 2013 at 15:50 Comment(0)
J
2

The best you can do is use the whiteLarge style. let i = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge).

Increasing the size of UIActivityIndicatorView does not change the size of the indicator proper, as you can see in these pictures.small indicator "large" indicator

Joust answered 2/6, 2018 at 19:31 Comment(0)
M
2

activityIndicator.transform = CGAffineTransform(scaleX: 1.75, y: 1.75);

This worked me for transforming size of indicator .

Maryjanemaryjo answered 29/2, 2020 at 2:36 Comment(0)
S
0

Yes, as it is already answered, visible size of UIActivityIndicatorView can be changed using transform property. To allow set/get exact indicator size, I have added simple extension:

extension UIActivityIndicatorView {

var imageSize: CGSize {
    let imgView = subviews.first { $0 is UIImageView }
    return imgView?.bounds.size ?? .zero
}

var radius: CGFloat {
    get {
        imageSize.width * scale / 2.0
    }
    set {
        let w = imageSize.width
        scale = (w == 0.0) ? 0 : newValue * 2.0 / w
    }
}

var scale: CGFloat {
    get {
        // just return x scale component as this var has meaning only
        // if transform of scale type, and x and y scales are same)
        transform.a
    }
    set {
        transform = CGAffineTransform(scaleX: newValue, y: newValue);
    }
}

}

With this extension you can simply write for example

indicatorView.radius = 16.0

It is also useful when you need to set exact spacing of indicator from some other view as UIActivityIndicatorView has zero frame.

Spectra answered 12/1, 2022 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.