Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position MBProgressHUD at the Bottom/Top of the screen

Is there a way to make the MBProgressHUD show at the bottom or top of the screen?

I have tried setting the frame using [hud setFrame:....];, initWithFrame, and setting thecenter property of the hud. None of these worked. I did an NSLog() of the frames after trying these methods. The values had changed but the hud still displayed at the center of the screen.

A thing to note, the hud is displayed using a UIWindow:

UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"some text";

Did this because the hud is displayed by a background execution block, thus the hud could be displayed on any view currently in display.

like image 632
kRiZ Avatar asked Nov 04 '14 05:11

kRiZ


2 Answers

I had the same trouble with a SVProgressHUD turns out I just had to add a new method

+ (void)setOffsetFromCenter:(UIOffset)offset {
[self sharedView].offsetFromCenter = offset;
}

and call it when I showed the HUD

[SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, 120)];

Mine just had to go a bit lower than mid-screen. Try it out, its a SingleTon so it's easier to call show and remove.

like image 79
Marcos Griselli Avatar answered Sep 30 '22 00:09

Marcos Griselli


We can change position of MBProgressHUD via change in yOffset and xOffset. Below line of code is for place MBProgressHUD at bottom.

let hud = MBProgressHUD.showAdded(to: view, animated: true)
hud?.mode = .text
hud?.labelText = json!["message"] as! String
hud?.yOffset = Float(view.frame.size.height*2/5);
hud?.removeFromSuperViewOnHide = true
hud?.margin = 10.0
hud?.hide(true, afterDelay: 2)
like image 31
Payal Maniyar Avatar answered Sep 29 '22 23:09

Payal Maniyar