Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rafattouqir/7554a92e43216215e598d28f4ec254eb to your computer and use it in GitHub Desktop.
Save rafattouqir/7554a92e43216215e598d28f4ec254eb to your computer and use it in GitHub Desktop.
Show an activity indicator in any UIView. Example: [imageView showActivityIndicator]; Then hide it [imageView hideActivityIndicator];
#import <Foundation/Foundation.h>
@interface UIView (ActivityIndicator)
- (void)showActivityIndicator;
- (void)showActivityIndicatorWithStyle:(UIActivityIndicatorViewStyle)style;
- (void)hideActivityIndicator;
@end
#import "UIView+ActivityIndicator.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIView (ActivityIndicator)
- (void)showActivityIndicator {
[self showActivityIndicatorWithStyle:UIActivityIndicatorViewStyleWhiteLarge];
}
- (void)showActivityIndicatorWithStyle:(UIActivityIndicatorViewStyle)style {
CGRect frame = self.frame;
frame.origin.x = 0;
frame.origin.y = 0;
UIView *view = [[UIView alloc] initWithFrame:frame];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
view.backgroundColor = [UIColor darkGrayColor];
view.layer.opacity = 0.5;
view.tag = 1001;
[self addSubview:view];
UIActivityIndicatorView* indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
indicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin
| UIViewAutoresizingFlexibleRightMargin
| UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleBottomMargin;
indicator.tag = 1002;
indicator.center = view.center;
[indicator startAnimating];
[self addSubview:indicator];
}
- (void)hideActivityIndicator {
[[self viewWithTag:1001] removeFromSuperview];
[[self viewWithTag:1002] removeFromSuperview];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment