react-native-navigation的迁移库

RNNCustomTitleView.m 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #import "RNNCustomTitleView.h"
  2. @interface RNNCustomTitleView ()
  3. @property (nonatomic, strong) RCTRootView *subView;
  4. @property (nonatomic, strong) NSString *alignment;
  5. @end
  6. @implementation RNNCustomTitleView
  7. - (instancetype)initWithFrame:(CGRect)frame subView:(RCTRootView*)subView alignment:(NSString*)alignment {
  8. self = [super init];
  9. if (self) {
  10. self.subView = subView;
  11. self.alignment = alignment;
  12. self.backgroundColor = [UIColor clearColor];
  13. self.subView.backgroundColor = [UIColor clearColor];
  14. if ([alignment isEqualToString:@"fill"]) {
  15. self.frame = frame;
  16. subView.sizeFlexibility = RCTRootViewSizeFlexibilityNone;
  17. } else {
  18. self.subView.delegate = self;
  19. subView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
  20. }
  21. [self addSubview:subView];
  22. }
  23. return self;
  24. }
  25. - (CGSize)sizeThatFits:(CGSize)size {
  26. if ([self.alignment isEqualToString:@"fill"]) {
  27. return size;
  28. }
  29. return [super sizeThatFits:size];
  30. }
  31. - (void)layoutSubviews {
  32. [super layoutSubviews];
  33. if ([self.alignment isEqualToString:@"fill"]) {
  34. [self.subView setFrame:self.frame];
  35. }
  36. }
  37. - (NSString *)alignment {
  38. return _alignment ? _alignment : @"center";
  39. }
  40. - (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
  41. if ([self.alignment isEqualToString:@"center"]) {
  42. [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.subView.intrinsicContentSize.width, self.subView.intrinsicContentSize.height)];
  43. [self.subView setFrame:CGRectMake(0, 0, rootView.intrinsicContentSize.width, rootView.intrinsicContentSize.height)];
  44. }
  45. }
  46. @end