react-native-navigation的迁移库

RNNNavigationButtons.m 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. @end
  8. @implementation RNNNavigationButtons
  9. -(instancetype)init {
  10. return [super init];
  11. }
  12. -(instancetype)initWithViewController:(RNNRootViewController*)viewController {
  13. self = [super init];
  14. self.viewController = viewController;
  15. return self;
  16. }
  17. -(void)applyLeftButtons:(NSArray*)leftButtons rightButtons:(NSArray*)rightButtons {
  18. if(leftButtons) {
  19. [self setButtons:leftButtons side:@"left" animated:NO];
  20. }
  21. if(rightButtons) {
  22. [self setButtons:rightButtons side:@"right" animated:NO];
  23. }
  24. }
  25. -(void)setButtons:(NSArray*)buttons side:(NSString*)side animated:(BOOL)animated {
  26. NSMutableArray *barButtonItems = [NSMutableArray new];
  27. for (NSDictionary *button in buttons) {
  28. RNNUIBarButtonItem* barButtonItem = [self buildButton:button];
  29. if(barButtonItem) {
  30. [barButtonItems addObject:barButtonItem];
  31. }
  32. }
  33. if ([side isEqualToString:@"left"])
  34. {
  35. [self.viewController.navigationItem setLeftBarButtonItems:barButtonItems animated:animated];
  36. }
  37. if ([side isEqualToString:@"right"])
  38. {
  39. [self.viewController.navigationItem setRightBarButtonItems:barButtonItems animated:animated];
  40. }
  41. }
  42. -(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary {
  43. NSString* buttonId = dictionary[@"id"];
  44. NSString* title = dictionary[@"title"];
  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 (iconImage) {
  55. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withIcon:iconImage];
  56. } else if (title) {
  57. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withTitle:title];
  58. NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:dictionary withPrefix:@"button"];
  59. if (buttonTextAttributes.allKeys.count > 0) {
  60. [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
  61. }
  62. } else {
  63. return nil;
  64. }
  65. barButtonItem.target = self;
  66. barButtonItem.action = @selector(onButtonPress:);
  67. NSNumber *disabled = dictionary[@"disabled"];
  68. BOOL disabledBool = disabled ? [disabled boolValue] : NO;
  69. if (disabledBool) {
  70. [barButtonItem setEnabled:NO];
  71. }
  72. NSNumber *disableIconTintString = dictionary[@"disableIconTint"];
  73. BOOL disableIconTint = disableIconTintString ? [disableIconTintString boolValue] : NO;
  74. if (disableIconTint) {
  75. [barButtonItem setImage:[barButtonItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  76. }
  77. NSString *testID = dictionary[@"testID"];
  78. if (testID)
  79. {
  80. barButtonItem.accessibilityIdentifier = testID;
  81. }
  82. return barButtonItem;
  83. }
  84. -(void)onButtonPress:(RNNUIBarButtonItem*)barButtonItem
  85. {
  86. [self.viewController.eventEmitter sendOnNavigationButtonPressed:self.viewController.containerId buttonId:barButtonItem.buttonId];
  87. }
  88. @end