react-native-navigation的迁移库

RNNUIBarButtonItem.m 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
  56. self.widthConstraint.constant = rootView.intrinsicContentSize.width;
  57. self.heightConstraint.constant = rootView.intrinsicContentSize.height;
  58. [rootView setNeedsUpdateConstraints];
  59. [rootView updateConstraintsIfNeeded];
  60. rootView.hidden = NO;
  61. }
  62. - (void)onButtonPressed {
  63. [self.target performSelector:self.action
  64. withObject:self
  65. afterDelay:0];
  66. }
  67. @end