react-native-navigation的迁移库

RNNUIBarButtonItem.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. @end
  9. @implementation RNNUIBarButtonItem
  10. -(instancetype)init:(NSString*)buttonId withIcon:(UIImage*)iconImage {
  11. UIButton* button = [[UIButton alloc] init];
  12. [button addTarget:self action:@selector(onButtonPressed) forControlEvents:UIControlEventTouchUpInside];
  13. [button setImage:iconImage forState:UIControlStateNormal];
  14. [button setFrame:CGRectMake(0, 0, iconImage.size.width, iconImage.size.height)];
  15. self = [super initWithCustomView:button];
  16. self.buttonId = buttonId;
  17. return self;
  18. }
  19. -(instancetype)init:(NSString*)buttonId withTitle:(NSString*)title {
  20. self = [super initWithTitle:title style:UIBarButtonItemStylePlain target:nil action:nil];
  21. self.buttonId = buttonId;
  22. return self;
  23. }
  24. -(instancetype)init:(NSString*)buttonId withCustomView:(RCTRootView *)reactView {
  25. self = [super initWithCustomView:reactView];
  26. reactView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
  27. reactView.delegate = self;
  28. reactView.backgroundColor = [UIColor clearColor];
  29. reactView.hidden = CGRectEqualToRect(reactView.frame, CGRectZero);
  30. [NSLayoutConstraint deactivateConstraints:reactView.constraints];
  31. self.widthConstraint = [NSLayoutConstraint constraintWithItem:reactView
  32. attribute:NSLayoutAttributeWidth
  33. relatedBy:NSLayoutRelationEqual
  34. toItem:nil
  35. attribute:NSLayoutAttributeNotAnAttribute
  36. multiplier:1.0
  37. constant:reactView.intrinsicContentSize.width];
  38. self.heightConstraint = [NSLayoutConstraint constraintWithItem:reactView
  39. attribute:NSLayoutAttributeHeight
  40. relatedBy:NSLayoutRelationEqual
  41. toItem:nil
  42. attribute:NSLayoutAttributeNotAnAttribute
  43. multiplier:1.0
  44. constant:reactView.intrinsicContentSize.height];
  45. [NSLayoutConstraint activateConstraints:@[self.widthConstraint, self.heightConstraint]];
  46. self.buttonId = buttonId;
  47. return self;
  48. }
  49. - (instancetype)init:(NSString*)buttonId withSystemItem:(NSString *)systemItemName {
  50. UIBarButtonSystemItem systemItem = [RCTConvert UIBarButtonSystemItem:systemItemName];
  51. self = [super initWithBarButtonSystemItem:systemItem target:nil action:nil];
  52. self.buttonId = buttonId;
  53. return self;
  54. }
  55. - (void)notifyDidAppear {
  56. if ([self.customView isKindOfClass:[RNNReactView class]]) {
  57. [((RNNReactView *)self.customView) componentDidAppear];
  58. }
  59. }
  60. - (void)notifyDidDisappear {
  61. if ([self.customView isKindOfClass:[RNNReactView class]]) {
  62. [((RNNReactView *)self.customView) componentDidDisappear];
  63. }
  64. }
  65. - (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
  66. self.widthConstraint.constant = rootView.intrinsicContentSize.width;
  67. self.heightConstraint.constant = rootView.intrinsicContentSize.height;
  68. [rootView setNeedsUpdateConstraints];
  69. [rootView updateConstraintsIfNeeded];
  70. rootView.hidden = NO;
  71. }
  72. - (void)onButtonPressed {
  73. [self.target performSelector:self.action
  74. withObject:self
  75. afterDelay:0];
  76. }
  77. @end