react-native-navigation的迁移库

RNNUIBarButtonItem.m 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. [NSLayoutConstraint deactivateConstraints:reactView.constraints];
  30. self.widthConstraint = [NSLayoutConstraint constraintWithItem:reactView
  31. attribute:NSLayoutAttributeWidth
  32. relatedBy:NSLayoutRelationEqual
  33. toItem:nil
  34. attribute:NSLayoutAttributeNotAnAttribute
  35. multiplier:1.0
  36. constant:reactView.intrinsicContentSize.width];
  37. self.heightConstraint = [NSLayoutConstraint constraintWithItem:reactView
  38. attribute:NSLayoutAttributeHeight
  39. relatedBy:NSLayoutRelationEqual
  40. toItem:nil
  41. attribute:NSLayoutAttributeNotAnAttribute
  42. multiplier:1.0
  43. constant:reactView.intrinsicContentSize.height];
  44. [NSLayoutConstraint activateConstraints:@[self.widthConstraint, self.heightConstraint]];
  45. self.buttonId = buttonId;
  46. return self;
  47. }
  48. - (instancetype)init:(NSString*)buttonId withSystemItem:(NSString *)systemItemName {
  49. UIBarButtonSystemItem systemItem = [RCTConvert UIBarButtonSystemItem:systemItemName];
  50. self = [super initWithBarButtonSystemItem:systemItem target:nil action:nil];
  51. self.buttonId = buttonId;
  52. return self;
  53. }
  54. - (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
  55. self.widthConstraint.constant = rootView.intrinsicContentSize.width;
  56. self.heightConstraint.constant = 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