react-native-navigation的迁移库

RNNUIBarButtonItem.m 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. self = [super initWithCustomView:button];
  15. self.buttonId = buttonId;
  16. return self;
  17. }
  18. -(instancetype)init:(NSString*)buttonId withTitle:(NSString*)title {
  19. self = [super initWithTitle:title style:UIBarButtonItemStylePlain target:nil action:nil];
  20. self.buttonId = buttonId;
  21. return self;
  22. }
  23. -(instancetype)init:(NSString*)buttonId withCustomView:(RCTRootView *)reactView {
  24. self = [super initWithCustomView:reactView];
  25. reactView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
  26. reactView.delegate = self;
  27. reactView.backgroundColor = [UIColor clearColor];
  28. self.widthConstraint = [NSLayoutConstraint constraintWithItem:reactView
  29. attribute:NSLayoutAttributeWidth
  30. relatedBy:NSLayoutRelationEqual
  31. toItem:nil
  32. attribute:NSLayoutAttributeNotAnAttribute
  33. multiplier:1.0
  34. constant:1.0];
  35. self.heightConstraint = [NSLayoutConstraint constraintWithItem:reactView
  36. attribute:NSLayoutAttributeHeight
  37. relatedBy:NSLayoutRelationEqual
  38. toItem:nil
  39. attribute:NSLayoutAttributeNotAnAttribute
  40. multiplier:1.0
  41. constant:1.0];
  42. [NSLayoutConstraint activateConstraints:@[self.widthConstraint, self.heightConstraint]];
  43. self.buttonId = buttonId;
  44. return self;
  45. }
  46. - (instancetype)init:(NSString*)buttonId withSystemItem:(NSString *)systemItemName {
  47. UIBarButtonSystemItem systemItem = [RCTConvert UIBarButtonSystemItem:systemItemName];
  48. self = [super initWithBarButtonSystemItem:systemItem target:nil action:nil];
  49. self.buttonId = buttonId;
  50. return self;
  51. }
  52. - (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
  53. self.widthConstraint.constant = rootView.intrinsicContentSize.width;
  54. self.heightConstraint.constant = rootView.intrinsicContentSize.height;
  55. [rootView setNeedsUpdateConstraints];
  56. [rootView updateConstraintsIfNeeded];
  57. }
  58. - (void)onButtonPressed {
  59. [self.target performSelector:self.action
  60. withObject:self
  61. afterDelay:0];
  62. }
  63. @end