react-native-navigation的迁移库

RNNNavigationButtons.m 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. if (!buttonId) {
  45. @throw [NSException exceptionWithName:@"NSInvalidArgumentException" reason:[@"button id is not specified " stringByAppendingString:title] userInfo:nil];
  46. }
  47. UIImage* iconImage = nil;
  48. id icon = dictionary[@"icon"];
  49. if (icon) {
  50. iconImage = [RCTConvert UIImage:icon];
  51. }
  52. RNNUIBarButtonItem *barButtonItem;
  53. if (iconImage) {
  54. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withIcon:iconImage];
  55. } else if (title) {
  56. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withTitle:title];
  57. NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:dictionary withPrefix:@"button"];
  58. if (buttonTextAttributes.allKeys.count > 0) {
  59. [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
  60. }
  61. } else {
  62. return nil;
  63. }
  64. barButtonItem.target = self;
  65. barButtonItem.action = @selector(onButtonPress:);
  66. NSNumber *disabled = dictionary[@"disabled"];
  67. BOOL disabledBool = disabled ? [disabled boolValue] : NO;
  68. if (disabledBool) {
  69. [barButtonItem setEnabled:NO];
  70. }
  71. NSNumber *disableIconTintString = dictionary[@"disableIconTint"];
  72. BOOL disableIconTint = disableIconTintString ? [disableIconTintString boolValue] : NO;
  73. if (disableIconTint) {
  74. [barButtonItem setImage:[barButtonItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  75. }
  76. id tintColor = dictionary[@"tintColor"];
  77. if (tintColor) {
  78. [barButtonItem setTintColor:[RCTConvert UIColor: tintColor]];
  79. }
  80. NSString *testID = dictionary[@"testID"];
  81. if (testID)
  82. {
  83. barButtonItem.accessibilityIdentifier = testID;
  84. }
  85. return barButtonItem;
  86. }
  87. -(void)onButtonPress:(RNNUIBarButtonItem*)barButtonItem {
  88. [self.viewController.eventEmitter sendOnNavigationButtonPressed:self.viewController.componentId buttonId:barButtonItem.buttonId];
  89. }
  90. @end