UIViewController+SPSBDefault.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // UIViewController+SPSBDefault.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/4/21.
  6. //
  7. #import "UIViewController+SPSBDefault.h"
  8. #import "SPSBNetworkManager.h"
  9. #import "UIView+SPSBDefault.h"
  10. #import <JXHViewMeasure.h>
  11. #import <objc/runtime.h>
  12. #import <JXHLayout.h>
  13. #import "SPSBConstantProfile.h"
  14. @implementation UIViewController (SPSBDefault)
  15. - (void)setDefaultViewWithError:(nullable NSError *)error image:(nullable UIImage *)image action:(void(^_Nullable)(void))action {
  16. if (self.spsb_defaultView) {
  17. [self.spsb_defaultView removeFromSuperview];
  18. }
  19. SPSBNetworkDataDefault type;
  20. if (error) {
  21. if (error.code == SPSBNetworkErrorCodeNetwrokMiss) {
  22. type = SPSBNetworkDataDefaultMissNetwork;
  23. } else {
  24. type = SPSBNetworkDataDefaultMissData;
  25. }
  26. } else {
  27. type = SPSBNetworkDataDefaultNoData;
  28. }
  29. self.spsb_defaultView = [self.view defultViewWithType:type image:image action:action];
  30. [self.view addSubview:self.spsb_defaultView];
  31. [self.spsb_defaultView makeConstraints:^(JXHConstraintMaker *make) {
  32. make.leading.and.trailing.equalTo(0);
  33. make.height.equalTo(self.spsb_defaultView.width);
  34. make.centerY.equalTo(self.view).offset(-jxh_viewHeight(self.view) * SPSBDefaultViewCenterYOffsetProportion);
  35. }];
  36. }
  37. - (void)removeDefaultView {
  38. if (self.spsb_defaultView) {
  39. [self.spsb_defaultView removeFromSuperview];
  40. self.spsb_defaultView = nil;
  41. }
  42. }
  43. - (nullable UIView *)spsb_defaultView {
  44. return objc_getAssociatedObject(self, @selector(setSpsb_defaultView:));
  45. }
  46. - (void)setSpsb_defaultView:(UIView *)spsb_defaultView {
  47. objc_setAssociatedObject(self, _cmd, spsb_defaultView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  48. }
  49. @end