iOS - Globally change MBProgressHUD design

2019-09-11 12:05发布

I use MBProgressHUD for loading screens in my app. I have 26 occurrences where I use a different HUD.

Now, I've decided to use a customised UIView (an animated UIImageView) for the HUD. I want to apply this to all the HUD's I use in my app, but the code to customise the HUD is about 15 lines long, and it's definitely not the right method to add this code to every single occurrence of a MBProgressHUD in my app.

What's the right way to go from here? This is not the first time I've run into an issue like this where I'm not sure how to keep my code cleaner and simpler.

3条回答
爷的心禁止访问
2楼-- · 2019-09-11 12:46

You can

1) subclass your MBProgressHUD so you don't touch your library you have embedded

2) Modify this properties and parameters in that subclass. It should be styled right here, as you don't have to do it in multiple places in your code.

3) Then call that subclass anywhere, as it would be that same class, you had called before.

查看更多
淡お忘
3楼-- · 2019-09-11 12:49

You could write a category on MBProgressHUD to return a customized instance of your HUD, and then import this to places where you use it, or in your AppName_Prefix.pch file to import it everywhere.

So, it would look something like this:

In MBProgressHUD+Additions.h:

+ (MBProgressHUD *)myCustomizedHUD;

In MBProgressHUD+Additions.m

+ (MBProgressHUD *)myCustomizedHUD {
   MBProgressHUD *myHUD = [[MBProgressHUD alloc] init]; 
   // customize 
   return myHud; 
}
查看更多
The star\"
4楼-- · 2019-09-11 12:51

One way is to declare the spinner in the AppDelegate, and write the methods showSpinner and hideSpinner, and also define a macro to call these spinner methods.

And you can just use this macro globally in your project.It is also easy to change the spinner code in the app delegate and you don't have to change anything.

查看更多
登录 后发表回答