Skip to content

Instantly share code, notes, and snippets.

@ryanmasondavies
Created January 13, 2016 16:28
Show Gist options
  • Save ryanmasondavies/93be4e23580ac1a95e7e to your computer and use it in GitHub Desktop.
Save ryanmasondavies/93be4e23580ac1a95e7e to your computer and use it in GitHub Desktop.
Generic loading view: UIView with centred UIActivityViewIndicator
#import <UIKit/UIKit.h>
@interface LoadingView : UIView
@property (nonatomic, weak, readonly) UILabel *loadingLabel;
@property (nonatomic, weak, readonly) UIActivityIndicatorView *activityIndicator;
@end
#import "LoadingView.h"
@interface LoadingView ()
@property (nonatomic, weak) UILabel *loadingLabel;
@property (nonatomic, weak) UIActivityIndicatorView *activityIndicator;
@end
@implementation LoadingView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.text = NSLocalizedString(@"Loading...", nil);
label.backgroundColor = [UIColor clearColor];
label.shadowOffset = CGSizeMake(0.0f, 1.0f);
label.shadowColor = [UIColor whiteColor];
[label sizeToFit];
[self addSubview:label];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.translatesAutoresizingMaskIntoConstraints = NO;
[indicator startAnimating];
[self addSubview:indicator];
self.loadingLabel = label;
self.activityIndicator = indicator;
[self addConstraint:[NSLayoutConstraint constraintWithItem:indicator
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:indicator
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:-10]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:10]];
}
return self;
}
- (void)layoutSubviews
{
[self.loadingLabel sizeToFit];
[super layoutSubviews];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment