问题
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