107

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;
Harshil Kotecha
  • 2,846
  • 4
  • 27
  • 41
wolverine
  • 2,967
  • 5
  • 31
  • 35

10 Answers10

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.

Andrew Vilcsak
  • 3,429
  • 2
  • 25
  • 14
  • 1
    and the activity indicators are much too small on the iPad with having one in the middle of a webView... – hokkuk Apr 08 '13 at 00:27
  • 5
    This seems to scale up the pixels, so not acceptable for large amounts, unfortunately. – prewett Feb 20 '14 at 09:12
90

Swift 3.0 & Swift 4.0

self.activityIndi.transform = CGAffineTransform(scaleX: 3, y: 3)
Harshil Kotecha
  • 2,846
  • 4
  • 27
  • 41
  • 4
    Exactly what I needed. This should be the top answer. – nocdib Jul 26 '18 at 20:12
  • 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 TV – Michele Dall'Agata Jul 29 '18 at 08:08
  • @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 performance – Harshil Kotecha Jul 30 '18 at 05:13
  • @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. – Michele Dall'Agata Jul 30 '18 at 06:39
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.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • 16
    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. – Ky - Jul 14 '15 at 21:00
  • 8
    @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. – meaning-matters Jan 19 '17 at 10:12
  • 3
    `.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 tiny – rockhammer Feb 14 '17 at 15:09
  • 1
    It will look blurry if you do a scaling transform to increase the size. – Gandalf458 Aug 28 '17 at 17:57
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.

vntstudy
  • 2,038
  • 20
  • 24
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)
aturan23
  • 4,798
  • 4
  • 28
  • 52
Vimal Saifudin
  • 1,815
  • 1
  • 21
  • 28
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)
CodeBender
  • 35,668
  • 12
  • 125
  • 132
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!

Valeriy
  • 71
  • 4
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

wsgeorge
  • 1,928
  • 17
  • 20
2

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

This worked me for transforming size of indicator .

Masoud Roosta
  • 455
  • 9
  • 19
Shah0651
  • 307
  • 3
  • 8
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.

Vladimir
  • 7,670
  • 8
  • 28
  • 42