react-native-navigation的迁移库

UITabBar+utils.m 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #import "UITabBar+utils.h"
  2. #import <objc/runtime.h>
  3. #define BADGE_OFFSET 0.2
  4. #define IMAGE_VIEW_TAG 1
  5. typedef void (*UITabBarButton_layoutSubviews__IMP)(void);
  6. static UITabBarButton_layoutSubviews__IMP original_UITabBarButton_layoutSubviews;
  7. @implementation UITabBar (utils)
  8. - (void)centerTabItems {
  9. [self removeTabBarItemTitles];
  10. [self swizzleUITabBarButton];
  11. }
  12. - (void)removeTabBarItemTitles {
  13. for (UITabBarItem* item in self.items) {
  14. item.title = nil;
  15. }
  16. }
  17. - (void)swizzleUITabBarButton {
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. [[self class] swizzleUITabBarButtonLayoutSubviews];
  21. });
  22. }
  23. + (void)swizzleUITabBarButtonLayoutSubviews {
  24. Class UITabBarButtonClass = NSClassFromString(@"UITabBarButton");
  25. SEL layoutSubviewsSEL = @selector(layoutSubviews);
  26. Method layoutSubviewsMethod = class_getInstanceMethod(UITabBarButtonClass, layoutSubviewsSEL);
  27. IMP layoutSubviewsIMP = method_getImplementation(layoutSubviewsMethod);
  28. original_UITabBarButton_layoutSubviews = layoutSubviewsIMP;
  29. SEL swizzleUITabBarButton_layoutSubviewsSEL = @selector(swizzleUITabBarButton_layoutSubviews);
  30. Method swizzleUITabBarButton_layoutSubviewsMethod = class_getInstanceMethod(self, swizzleUITabBarButton_layoutSubviewsSEL);
  31. method_exchangeImplementations(layoutSubviewsMethod, swizzleUITabBarButton_layoutSubviewsMethod);
  32. }
  33. - (void)swizzleUITabBarButton_layoutSubviews {
  34. original_UITabBarButton_layoutSubviews();
  35. for (UIView *subView in self.subviews) {
  36. if ([subView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
  37. subView.center = CGPointMake(subView.center.x, subView.superview.frame.size.height / 2);
  38. subView.tag = IMAGE_VIEW_TAG;
  39. }
  40. if ([subView isKindOfClass:NSClassFromString(@"_UIBadgeView")]) {
  41. UIView* imageView = [subView.superview viewWithTag:IMAGE_VIEW_TAG];
  42. subView.frame = CGRectMake(subView.frame.origin.x, (imageView.frame.origin.y + imageView.frame.size.height * BADGE_OFFSET) - subView.frame.size.height / 2, subView.frame.size.width, subView.frame.size.height);
  43. }
  44. }
  45. }
  46. @end