MBProgressHUD无法覆盖键盘

时间:2011-10-18 01:54:51

标签: ios mbprogresshud

当我在键盘显示时尝试显示MBProgressHUD时,我使用下面的代码,但HUD对象无法覆盖键盘:

SNSSharerAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
HUD = [[MBProgressHUD showHUDAddedTo:delegate.window animated:YES] retain];
HUD.mode = MBProgressHUDModeIndeterminate;
HUD.labelText = @"Posting...";
[HUD show:YES];

我认为HUD对象显示在委托窗口的前面,键盘也显示,所以最后添加了,这是前面的。我错了吗?

2 个答案:

答案 0 :(得分:17)

将hud添加到包含键盘的第二个窗口。 键盘显示时,应用程序中有两个UIWindow实例。第一个是常规窗口,第二个是临时键盘窗口。 代码:

UIWindow *tempKeyboardWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
MBProgressHUD *hud=[[MBProgressHUD alloc] initWithWindow:tempKeyboardWindow];
hub.mode=MBProgressHUDModeIndeterminate;
hub.labelText=@"Sending...";
[tempKeyboardWindow addSubview:hud];
[hud show:YES];

在ios4.3和ios5.x中测试过,它确实有效。

答案 1 :(得分:2)

for ios 9而不是[[[UIApplication sharedApplication] windows] objectAtIndex:1]尝试使用[[[UIApplication sharedApplication] windows] lastObject]

所以它会像这样

UIWindow *tempKeyboardWindow = [[[UIApplication sharedApplication] windows] lastObject];
MBProgressHUD *hud=[[MBProgressHUD alloc] initWithWindow:tempKeyboardWindow];
hub.mode=MBProgressHUDModeIndeterminate;
hub.labelText=@"Sending...";
[tempKeyboardWindow addSubview:hud];
[hud show:YES];