react-native-navigation的迁移库

RNNUIBarButtonItem.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. self.widthConstraint = [NSLayoutConstraint constraintWithItem:reactView
  30. attribute:NSLayoutAttributeWidth
  31. relatedBy:NSLayoutRelationEqual
  32. toItem:nil
  33. attribute:NSLayoutAttributeNotAnAttribute
  34. multiplier:1.0
  35. constant:1.0];
  36. self.heightConstraint = [NSLayoutConstraint constraintWithItem:reactView
  37. attribute:NSLayoutAttributeHeight
  38. relatedBy:NSLayoutRelationEqual
  39. toItem:nil
  40. attribute:NSLayoutAttributeNotAnAttribute
  41. multiplier:1.0
  42. constant:1.0];
  43. [NSLayoutConstraint activateConstraints:@[self.widthConstraint, self.heightConstraint]];
  44. self.buttonId = buttonId;
  45. return self;
  46. }
  47. - (instancetype)init:(NSString*)buttonId withSystemItem:(NSString *)systemItemName {
  48. UIBarButtonSystemItem systemItem = [RCTConvert UIBarButtonSystemItem:systemItemName];
  49. self = [super initWithBarButtonSystemItem:systemItem target:nil action:nil];
  50. self.buttonId = buttonId;
  51. return self;
  52. }
  53. - (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
  54. self.widthConstraint.constant = rootView.intrinsicContentSize.width;
  55. self.heightConstraint.constant = rootView.intrinsicContentSize.height;
  56. [rootView setFrame:CGRectMake(0, 0, rootView.intrinsicContentSize.width, rootView.intrinsicContentSize.height)];
  57. [rootView setNeedsUpdateConstraints];
  58. [rootView updateConstraintsIfNeeded];
  59. }
  60. - (void)onButtonPressed {
  61. [self.target performSelector:self.action
  62. withObject:self
  63. afterDelay:0];
  64. }
  65. @end