react-native-navigation的迁移库

RNNNavigationButtons.m 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. NSDictionary* 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[@"name"] rootViewId:component[@"componentId"]];
  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. NSMutableDictionary* textAttributes = [[NSMutableDictionary alloc] init];
  79. NSMutableDictionary* disabledTextAttributes = [[NSMutableDictionary alloc] init];
  80. id color = dictionary[@"color"];
  81. if (color) {
  82. [textAttributes setObject:[RCTConvert UIColor:color] forKey:NSForegroundColorAttributeName];
  83. }
  84. NSNumber* disabledColor = dictionary[@"disabledColor"];
  85. if (disabledColor) {
  86. UIColor *color = [RCTConvert UIColor:disabledColor];
  87. [disabledTextAttributes setObject:color forKey:NSForegroundColorAttributeName];
  88. }
  89. NSNumber* fontSize = dictionary[@"fontSize"] ? dictionary[@"fontSize"] : @(17);
  90. NSString* fontFamily = dictionary[@"fontFamily"];
  91. if (fontFamily) {
  92. [textAttributes setObject:[UIFont fontWithName:fontFamily size:[fontSize floatValue]] forKey:NSFontAttributeName];
  93. } else{
  94. [textAttributes setObject:[UIFont systemFontOfSize:[fontSize floatValue]] forKey:NSFontAttributeName];
  95. }
  96. [barButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
  97. [barButtonItem setTitleTextAttributes:disabledTextAttributes forState:UIControlStateDisabled];
  98. NSString *testID = dictionary[@"testID"];
  99. if (testID)
  100. {
  101. barButtonItem.accessibilityIdentifier = testID;
  102. }
  103. return barButtonItem;
  104. }
  105. -(void)onButtonPress:(RNNUIBarButtonItem*)barButtonItem {
  106. [self.viewController.eventEmitter sendOnNavigationButtonPressed:self.viewController.componentId buttonId:barButtonItem.buttonId];
  107. }
  108. @end