iOS - Globally change MBProgressHUD design

天涯浪子 提交于 2020-01-25 20:44:07

问题


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.


回答1:


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.




回答2:


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:


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; 
}


来源:https://stackoverflow.com/questions/39317179/ios-globally-change-mbprogresshud-design

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!