我希望显示加载屏幕,直到从服务器获取数据为止。为此,我使用此代码https://gist.github.com/igordeoliveirasa/78a310f0348fcad9b270创建带有活动指示符的叠加层。
但这只有在我通过调用
在基础NavigationController
屏幕上添加叠加层时才有效
LoadingOverlay.shared.showOverlay(self.navigationController?.view)
即使我的导航栏也覆盖了叠加层,用户也无法返回,以防页面需要更长时间才能加载。如果我尝试使用以下任何事情都没有发生。
LoadingOverlay.shared.showOverlay(self.view)
非常感谢任何提示或解决方案。
编辑 - 我正在尝试创建类似下面的内容,即使加载覆盖导航栏和标签栏仍会出现,用户可以对其进行任何操作
但是在使用我上面提供的链接的代码,以及@Cute Angel的代码时,我得到的是下面的内容,其中覆盖确实会出现,但它不会超过我在基本视图中添加的元素来自auto布局如果我使用self.view
如何在我ViewController
的基本视图中添加的所有元素之上添加此叠加层,并保留导航栏和标签栏不带叠加层
我想要实现的是下面的内容
答案 0 :(得分:0)
请尝试以下代码:
声明宏
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
在.h
@property (strong, nonatomic) UIActivityIndicatorView *indicator;
@property (strong, nonatomic) UIView *processView;
#pragma mark - Process view
- (void)showProcess:(UIView *)view;
- (void)hideProcess;
在.m
@synthesize indicator, processView;
#pragma mark - Process view
- (void)showProcess:(UIView *)view
{
if (self.processView == nil)
{
self.processView = [[UIView alloc] initWithFrame:view.frame]; // If you want to cover whole screen then apply [[UIScreen mainScreen] bounds]] as a frame
self.processView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
//self.processView.userInteractionEnabled = FALSE;
UIView *viewInner = [[UIView alloc] init];
if (IS_IPHONE)
viewInner.frame = CGRectMake(0, 0, 90, 90);
else
viewInner.frame = CGRectMake(0, 0, 150, 150);
viewInner.center = self.processView.center;
viewInner.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
[viewInner.layer setCornerRadius:10.0f];
[viewInner.layer setMasksToBounds:YES];
self.indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:IS_IPHONE?UIActivityIndicatorViewStyleWhite:UIActivityIndicatorViewStyleWhiteLarge];
self.indicator.frame = CGRectMake(0, IS_IPHONE?-15:-30, viewInner.frame.size.width, viewInner.frame.size.height);
self.indicator.hidesWhenStopped = YES;
[viewInner addSubview:self.indicator];
UILabel *lblLoading = [[UILabel alloc] initWithFrame:CGRectMake(5, viewInner.frame.size.height-(IS_IPHONE?45:70), viewInner.frame.size.width-10, IS_IPHONE?40:70)];
lblLoading.backgroundColor = [UIColor clearColor];
lblLoading.textColor = [UIColor whiteColor];
lblLoading.textAlignment = NSTextAlignmentCenter;
lblLoading.adjustsFontSizeToFitWidth = TRUE;
lblLoading.font = [UIFont systemFontOfSize:IS_IPHONE?15.0f:24.0f];
lblLoading.text = @"Loading...";
[viewInner addSubview:lblLoading];
[self.processView addSubview:viewInner];
}
[view addSubview:self.processView]; // If you want to cover whole screen then add it to [self.window addSubview:self.processView]
[self performSelector:@selector(createProcess) withObject:nil afterDelay:0.1];
}
- (void)createProcess
{
[self.indicator startAnimating];
}
- (void)hideProcess
{
[self.indicator performSelector:@selector(stopAnimating) withObject:nil afterDelay:0.5];
[self.processView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.5];
}
当您想要显示和隐藏
时YourClassName * classObject = [[YourClassName alloc] init];
[classObject showProcess:self.view];
[classObject hideProcess];
对于斯威夫特: 1.声明变量
var processView: UIView?
var indicator: UIActivityIndicatorView?
2。定义方法
// MARK: - Process view
func showProcess(view:UIView) {
UIScreen.mainScreen().bounds
self.processView = UIView(frame: view.frame) // // If you want to cover whole screen then apply UIScreen.mainScreen().bounds as frame
self.processView?.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.4)
let viewInner = UIView()
let lblLoading = UILabel()
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone {
viewInner.frame = CGRectMake(0, 0, 90, 90)
self.indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
self.indicator!.frame = CGRectMake(0, -15, viewInner.frame.size.width, viewInner.frame.size.height)
lblLoading.frame = CGRectMake(5, viewInner.frame.size.height-45, viewInner.frame.size.width-10, 40)
lblLoading.font = UIFont.systemFontOfSize(15.0)
}
else {
viewInner.frame = CGRectMake(0, 0, 150, 150)
self.indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
self.indicator!.frame = CGRectMake(0, -30, viewInner.frame.size.width, viewInner.frame.size.height)
lblLoading.frame = CGRectMake(5, viewInner.frame.size.height-70, viewInner.frame.size.width-10, 70)
lblLoading.font = UIFont.systemFontOfSize(24.0)
}
viewInner.center = self.processView!.center
viewInner.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
viewInner.layer.cornerRadius = 10.0
viewInner.layer.masksToBounds = true
self.indicator!.hidesWhenStopped = true
viewInner.addSubview(self.indicator!)
lblLoading.backgroundColor = UIColor.clearColor()
lblLoading.textColor = UIColor.whiteColor()
lblLoading.textAlignment = NSTextAlignment.Center
lblLoading.adjustsFontSizeToFitWidth = true
lblLoading.text = "Loading..."
viewInner.addSubview(lblLoading)
self.processView!.addSubview(viewInner)
view.addSubview(self.processView!) // If you want to cover whole screen then add it to self.window.addSubview(self.processView)
self.performSelector("createProcess", withObject: nil, afterDelay: 0.1)
}
func createProcess() {
self.indicator?.startAnimating()
}
func hideProcess() {
self.indicator?.performSelector("stopAnimating", withObject: nil, afterDelay: 0.5)
self.processView?.performSelector("removeFromSuperview", withObject: nil, afterDelay: 0.5)
}
在需要时打电话
var classObject = YourClass()
classObject.showProcess(self.view)
classObject.hideProcess()