Can I change the size of UIActivityIndicator?

IosIphoneSwiftUiactivityindicatorview

Ios Problem Overview


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;

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

Swift 3.0 & Swift 4.0

self.activityIndi.transform = CGAffineTransform(scaleX: 3, y: 3)

Solution 3 - Ios

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.

Solution 4 - Ios

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.

Solution 5 - Ios

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)

Solution 6 - Ios

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)

Solution 7 - Ios

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!

Solution 8 - Ios

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

Solution 9 - Ios

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

This worked me for transforming size of indicator .

Solution 10 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwolverineView Question on Stackoverflow
Solution 1 - IosAndrew VilcsakView Answer on Stackoverflow
Solution 2 - IosHarshil KotechaView Answer on Stackoverflow
Solution 3 - IosTechZenView Answer on Stackoverflow
Solution 4 - IosvntstudyView Answer on Stackoverflow
Solution 5 - IosVimal SaifudinView Answer on Stackoverflow
Solution 6 - IosCodeBenderView Answer on Stackoverflow
Solution 7 - IosValeriyView Answer on Stackoverflow
Solution 8 - IoswsgeorgeView Answer on Stackoverflow
Solution 9 - IosShah0651View Answer on Stackoverflow
Solution 10 - IosVladimirView Answer on Stackoverflow