react-native-navigation的迁移库

RNNUIBarButtonItem.m 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 = YES;
  30. self.widthConstraint = [NSLayoutConstraint constraintWithItem:reactView
  31. attribute:NSLayoutAttributeWidth
  32. relatedBy:NSLayoutRelationEqual
  33. toItem:nil
  34. attribute:NSLayoutAttributeNotAnAttribute
  35. multiplier:1.0
  36. constant:1.0];
  37. self.heightConstraint = [NSLayoutConstraint constraintWithItem:reactView
  38. attribute:NSLayoutAttributeHeight
  39. relatedBy:NSLayoutRelationEqual
  40. toItem:nil
  41. attribute:NSLayoutAttributeNotAnAttribute
  42. multiplier:1.0
  43. constant:1.0];
  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. rootView.hidden = NO;
  56. self.widthConstraint.constant = rootView.intrinsicContentSize.width;
  57. self.heightConstraint.constant = rootView.intrinsicContentSize.height;
  58. [rootView setNeedsUpdateConstraints];
  59. [rootView updateConstraintsIfNeeded];
  60. }
  61. - (void)onButtonPressed {
  62. [self.target performSelector:self.action
  63. withObject:self
  64. afterDelay:0];
  65. }
  66. @end