react-native-navigation的迁移库

RNNUIBarButtonItem.m 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>
  3. #import "RNNUIBarButtonItem.h"
  4. #import "RCTConvert+UIBarButtonSystemItem.h"
  5. @interface RNNUIBarButtonItem ()
  6. @property (nonatomic, strong) NSLayoutConstraint *widthConstraint;
  7. @property (nonatomic, strong) NSLayoutConstraint *heightConstraint;
  8. @property (nonatomic, weak) RNNReactComponentRegistry *componentRegistry;
  9. @end
  10. @implementation RNNUIBarButtonItem
  11. -(instancetype)init:(NSString*)buttonId withIcon:(UIImage*)iconImage {
  12. UIButton* button = [[UIButton alloc] init];
  13. [button addTarget:self action:@selector(onButtonPressed) forControlEvents:UIControlEventTouchUpInside];
  14. [button setImage:iconImage forState:UIControlStateNormal];
  15. [button setFrame:CGRectMake(0, 0, iconImage.size.width, iconImage.size.height)];
  16. self = [super initWithCustomView:button];
  17. self.buttonId = buttonId;
  18. return self;
  19. }
  20. -(instancetype)init:(NSString*)buttonId withTitle:(NSString*)title {
  21. self = [super initWithTitle:title style:UIBarButtonItemStylePlain target:nil action:nil];
  22. self.buttonId = buttonId;
  23. return self;
  24. }
  25. -(instancetype)init:(NSString*)buttonId withCustomView:(RCTRootView *)reactView componentRegistry:(RNNReactComponentRegistry *)componentRegistry {
  26. self = [super initWithCustomView:reactView];
  27. self.componentRegistry = componentRegistry;
  28. reactView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
  29. reactView.delegate = self;
  30. reactView.backgroundColor = [UIColor clearColor];
  31. reactView.hidden = YES;
  32. self.widthConstraint = [NSLayoutConstraint constraintWithItem:reactView
  33. attribute:NSLayoutAttributeWidth
  34. relatedBy:NSLayoutRelationEqual
  35. toItem:nil
  36. attribute:NSLayoutAttributeNotAnAttribute
  37. multiplier:1.0
  38. constant:1.0];
  39. self.heightConstraint = [NSLayoutConstraint constraintWithItem:reactView
  40. attribute:NSLayoutAttributeHeight
  41. relatedBy:NSLayoutRelationEqual
  42. toItem:nil
  43. attribute:NSLayoutAttributeNotAnAttribute
  44. multiplier:1.0
  45. constant:1.0];
  46. [NSLayoutConstraint activateConstraints:@[self.widthConstraint, self.heightConstraint]];
  47. self.buttonId = buttonId;
  48. return self;
  49. }
  50. - (instancetype)init:(NSString*)buttonId withSystemItem:(NSString *)systemItemName {
  51. UIBarButtonSystemItem systemItem = [RCTConvert UIBarButtonSystemItem:systemItemName];
  52. self = [super initWithBarButtonSystemItem:systemItem target:nil action:nil];
  53. self.buttonId = buttonId;
  54. return self;
  55. }
  56. - (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
  57. rootView.hidden = NO;
  58. self.widthConstraint.constant = rootView.intrinsicContentSize.width;
  59. self.heightConstraint.constant = rootView.intrinsicContentSize.height;
  60. [rootView setNeedsUpdateConstraints];
  61. [rootView updateConstraintsIfNeeded];
  62. }
  63. - (void)onButtonPressed {
  64. [self.target performSelector:self.action
  65. withObject:self
  66. afterDelay:0];
  67. }
  68. - (void)dealloc {
  69. if ([self.customView isKindOfClass:[RNNReactView class]]) {
  70. RNNReactView* customView = self.customView;
  71. [self.componentRegistry removeChildComponent:customView.componentId];
  72. }
  73. }
  74. @end