Reputation: 2600
I extended the UIView
class to create an overlay load screen.
FullLoadingView.h
#import <UIKit/UIKit.h>
@interface FullLoadingView : UIView
+ (instancetype)showOnView:(UIView *)view;
+ (void)hideAll:(UIView *)view;
- (void)hide;
@end
FullLoadingView.m
#import "FullLoadingView.h"
@implementation FullLoadingView
+ (instancetype)showOnView:(UIView *)view
{
FullLoadingView *fullLoadingView = [[self alloc] init];
[fullLoadingView setFrame:view.bounds];
[fullLoadingView setBackgroundColor:[UIColor colorWithRed:(250.0/255.0) green:(250.0/255.0) blue:(250.0/255.0) alpha:1.0]];
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicatorView.center = fullLoadingView.center;
[activityIndicatorView startAnimating];
[view addSubview:fullLoadingView];
return fullLoadingView;
}
- (void)hide
{
[self removeFromSuperview];
}
+ (void)hideAll:(UIView *)view
{
NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
for (UIView *subview in subviewsEnum) {
if ([subview isKindOfClass:self]) {
FullLoadingView *fullLoadingView = (FullLoadingView *)subview;
[fullLoadingView removeFromSuperview];
}
}
}
@end
UIViewController code:
- (void)viewDidLoad {
FullLoadingView *fullLoadingView = [FullLoadingView showOnView:self.view];
}
Issue:
When the UIViewController
has NavigationBar
or TabBar
the central alignment of the UIActivityIndicatorView
is not correct.
PS: The self.view
has the correct height (excludingNavigationBar
or TabBar
) butFullLoadingView
always has full height of the device.
Upvotes: 0
Views: 37
Reputation: 385500
You're creating fullLoadingView
in viewDidLoad
, but self.view
's frame is not guaranteed to be correct for the current device until viewDidLayoutSubviews
.
Probably the simplest fix is to use autoresizingMask
to keep the frames in sync:
+ (instancetype)showOnView:(UIView *)view
{
FullLoadingView *fullLoadingView = [[self alloc] init];
fullLoadingView.frame = view.bounds;
fullLoadingView.autoresizingMask = (
UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleHeight);
fullLoadingView.backgroundColor = [UIColor colorWithRed:(250.0/255.0) green:(250.0/255.0) blue:(250.0/255.0) alpha:1.0];
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicatorView.center = fullLoadingView.center;
activityIndicatorView.autoresizingMask = (
UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleBottomMargin
| UIViewAutoresizingFlexibleLeftMargin
| UIViewAutoresizingFlexibleRightMargin);
[fullLoadingView addSubview:activityIndicatorView];
[activityIndicatorView startAnimating];
[view addSubview:fullLoadingView];
return fullLoadingView;
}
Upvotes: 2