我可以更改 UIActivityIndicator 的大小吗?

无论我给它的大小,而分配,它只显示固定的大小。是否可能增加它?

密码:

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;
81920 次浏览

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.

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.

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!

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.

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)

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)

Swift 3.0 & Swift 4.0

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

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

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

This worked me for transforming size of indicator .

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.