react-native-navigation的迁移库

RNNNavigationButtons.m 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #import "RNNNavigationButtons.h"
  2. #import "RNNUIBarButtonItem.h"
  3. #import <React/RCTConvert.h>
  4. #import "RCTHelpers.h"
  5. @interface RNNNavigationButtons()
  6. @property (weak, nonatomic) RNNRootViewController* viewController;
  7. @property (strong, nonatomic) NSArray* rightButtons;
  8. @property (strong, nonatomic) NSArray* leftButtons;
  9. @end
  10. @implementation RNNNavigationButtons
  11. -(instancetype)initWithViewController:(RNNRootViewController*)viewController {
  12. self = [super init];
  13. self.viewController = viewController;
  14. return self;
  15. }
  16. -(void)applyLeftButtons:(NSArray*)leftButtons rightButtons:(NSArray*)rightButtons {
  17. if (leftButtons) {
  18. [self setButtons:leftButtons side:@"left" animated:NO];
  19. }
  20. if (rightButtons) {
  21. [self setButtons:rightButtons side:@"right" animated:NO];
  22. }
  23. }
  24. -(void)setButtons:(NSArray*)buttons side:(NSString*)side animated:(BOOL)animated {
  25. NSMutableArray *barButtonItems = [NSMutableArray new];
  26. for (NSDictionary *button in buttons) {
  27. RNNUIBarButtonItem* barButtonItem = [self buildButton:button];
  28. if(barButtonItem) {
  29. [barButtonItems addObject:barButtonItem];
  30. }
  31. }
  32. if ([side isEqualToString:@"left"]) {
  33. self.leftButtons = barButtonItems;
  34. [self.viewController.navigationItem setLeftBarButtonItems:self.leftButtons animated:animated];
  35. }
  36. if ([side isEqualToString:@"right"]) {
  37. self.rightButtons = barButtonItems;
  38. [self.viewController.navigationItem setRightBarButtonItems:self.rightButtons animated:animated];
  39. }
  40. }
  41. -(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary {
  42. NSString* buttonId = dictionary[@"id"];
  43. NSString* title = dictionary[@"title"];
  44. NSString* component = dictionary[@"component"];
  45. if (!buttonId) {
  46. @throw [NSException exceptionWithName:@"NSInvalidArgumentException" reason:[@"button id is not specified " stringByAppendingString:title] userInfo:nil];
  47. }
  48. UIImage* iconImage = nil;
  49. id icon = dictionary[@"icon"];
  50. if (icon) {
  51. iconImage = [RCTConvert UIImage:icon];
  52. }
  53. RNNUIBarButtonItem *barButtonItem;
  54. if (component) {
  55. RCTRootView *view = (RCTRootView*)[self.viewController.creator createRootView:component rootViewId:buttonId];
  56. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withCustomView:view];
  57. } else if (iconImage) {
  58. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withIcon:iconImage];
  59. } else if (title) {
  60. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withTitle:title];
  61. NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:dictionary withPrefix:@"button"];
  62. if (buttonTextAttributes.allKeys.count > 0) {
  63. [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
  64. }
  65. } else {
  66. return nil;
  67. }
  68. barButtonItem.target = self;
  69. barButtonItem.action = @selector(onButtonPress:);
  70. NSNumber *enabled = dictionary[@"enabled"];
  71. BOOL enabledBool = enabled ? [enabled boolValue] : YES;
  72. [barButtonItem setEnabled:enabledBool];
  73. NSNumber *disableIconTintString = dictionary[@"disableIconTint"];
  74. BOOL disableIconTint = disableIconTintString ? [disableIconTintString boolValue] : NO;
  75. if (disableIconTint) {
  76. [barButtonItem setImage:[barButtonItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  77. }
  78. id tintColor = dictionary[@"tintColor"];
  79. if (tintColor) {
  80. [barButtonItem setTintColor:[RCTConvert UIColor: tintColor]];
  81. }
  82. NSNumber* disabledColor = dictionary[@"disabledColor"];
  83. if (disabledColor) {
  84. UIColor *color = [RCTConvert UIColor:disabledColor];
  85. [barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : color} forState:UIControlStateDisabled];
  86. }
  87. NSString *testID = dictionary[@"testID"];
  88. if (testID)
  89. {
  90. barButtonItem.accessibilityIdentifier = testID;
  91. }
  92. return barButtonItem;
  93. }
  94. -(void)onButtonPress:(RNNUIBarButtonItem*)barButtonItem {
  95. [self.viewController.eventEmitter sendOnNavigationButtonPressed:self.viewController.componentId buttonId:barButtonItem.buttonId];
  96. }
  97. @end