Ios 在除导航栏-Swift之外的整个屏幕上添加加载覆盖
我想显示一个加载屏幕,直到从服务器获取数据为止。为此,我使用此代码创建一个带有活动指示器的覆盖 但这只有在我通过调用Ios 在除导航栏-Swift之外的整个屏幕上添加加载覆盖,ios,swift,Ios,Swift,我想显示一个加载屏幕,直到从服务器获取数据为止。为此,我使用此代码创建一个带有活动指示器的覆盖 但这只有在我通过调用 LoadingOverlay.shared.showOverlay(self.navigationController?.view) 即使是我的导航栏也会被覆盖,用户也无法返回,以防页面加载时间过长。如果我尝试使用下面的方法,什么都不会发生 LoadingOverlay.shared.showOverlay(self.view) 任何提示或解决方案都将不胜感激 编辑-我正在尝
LoadingOverlay.shared.showOverlay(self.navigationController?.view)
即使是我的导航栏也会被覆盖,用户也无法返回,以防页面加载时间过长。如果我尝试使用下面的方法,什么都不会发生
LoadingOverlay.shared.showOverlay(self.view)
任何提示或解决方案都将不胜感激
编辑-我正在尝试创建如下内容,在加载覆盖后,导航栏和选项卡栏仍然会出现,用户可以对其进行任何操作
但是在使用我上面提供的链接代码,以及@Cute Angel的代码时,我得到的结果如下所示,如果我使用self.view
如何在我在ViewController
的基本视图中添加的所有元素之上获得此覆盖,并使导航栏和选项卡栏不具有覆盖
我想要达到的目标如下
尝试以下代码:
#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];
var processView: UIView?
var indicator: UIActivityIndicatorView?
二,。定义方法
// 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()
请尝试以下代码:
#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];
var processView: UIView?
var indicator: UIActivityIndicatorView?
二,。定义方法
// 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()
谢谢,但我使用的是Swift 2,你能给我一些建议吗?我在使用
showProcess
和hideProcess
方法时出错,AppDelegate
分别没有成员showProcess
和AppDelegate
没有成员hideProcess
您是否完美地实现了此代码?请再次检查。使用类对象调用定义方法的方法我在代码中遇到的名称问题。覆盖即将出现,它位于导航栏下方(如需要),但它不位于我在视图中添加的元素顶部,如UITableView
,UIImageView
等。我使用AutoLayoutHanks在基本视图顶部添加了这些元素,但我使用的是Swift 2,你能给我一些建议吗?我在showProcess
和hideProcess
方法中出错,AppDelegate
没有成员showProcess
和AppDelegate
没有成员hideProcess
你是否完美地实现了这段代码?请再次检查。使用类对象调用定义方法的方法我在代码中遇到的名称问题。覆盖即将出现,它位于导航栏下方(如需要),但它不会出现在我在视图中添加的元素顶部,如我使用AutoLayout在基础视图顶部添加的UITableView
、UIImageView
等