react-native-navigation的迁移库

RCCCustomTitleView.m 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // RCCTitleView.m
  3. // ReactNativeNavigation
  4. //
  5. // Created by Ran Greenberg on 26/04/2017.
  6. // Copyright © 2017 artal. All rights reserved.
  7. //
  8. #import "RCCCustomTitleView.h"
  9. @interface RCCCustomTitleView ()
  10. @property (nonatomic, strong) UIView *subView;
  11. @property (nonatomic, strong) NSString *subViewAlign;
  12. @property float initialWidth;
  13. @end
  14. @implementation RCCCustomTitleView
  15. -(instancetype)initWithFrame:(CGRect)frame subView:(UIView*)subView alignment:(NSString*)alignment {
  16. _initialWidth = frame.size.width;
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. self.backgroundColor = [UIColor clearColor];
  20. self.subView = subView;
  21. self.subViewAlign = alignment;
  22. subView.frame = self.bounds;
  23. [self addSubview:subView];
  24. }
  25. return self;
  26. }
  27. -(void)layoutSubviews {
  28. [super layoutSubviews];
  29. if ([self.subViewAlign isEqualToString:@"fill"]) {
  30. self.subView.frame = self.bounds;
  31. }
  32. else {
  33. CGFloat superViewWidth = self.superview.frame.size.width;
  34. CGFloat paddingLeftFromCenter = (superViewWidth/2) - self.frame.origin.x;
  35. CGFloat paddingRightFromCenter = self.frame.size.width - paddingLeftFromCenter;;
  36. CGRect reactViewFrame = self.subView.bounds;
  37. CGFloat minPadding = MIN(paddingLeftFromCenter, paddingRightFromCenter);
  38. reactViewFrame.size.width = minPadding*2;
  39. reactViewFrame.origin.x = paddingLeftFromCenter - minPadding;
  40. self.subView.frame = reactViewFrame;
  41. }
  42. }
  43. - (void)setFrame:(CGRect) frame {
  44. float referenceWidth = [self statusBarWidth];
  45. if (referenceWidth == 0) {
  46. referenceWidth = _initialWidth;
  47. }
  48. float newNavBarWidth = frame.size.width;
  49. BOOL frameNeedsToBeCorrected = newNavBarWidth < referenceWidth || CGRectEqualToRect(self.frame, CGRectZero);
  50. if (frameNeedsToBeCorrected) {
  51. // first we need to find out the total point diff of the status bar and the nav bar
  52. float navBarHorizontalMargin = referenceWidth - newNavBarWidth;
  53. CGRect correctedFrame = frame;
  54. // then we need to place the nav bar half times the horizontal margin to the left
  55. correctedFrame.origin.x = -(navBarHorizontalMargin / 2);
  56. // and finally set the width so that it's equal to the status bar width
  57. correctedFrame.size.width = referenceWidth;
  58. [super setFrame:correctedFrame];
  59. } else if (frame.size.height != self.frame.size.height) { // otherwise
  60. // if only the height has changed
  61. CGRect newHeightFrame = self.frame;
  62. // make sure we update just the height
  63. newHeightFrame.size.height = frame.size.height;
  64. [super setFrame:newHeightFrame];
  65. }
  66. // keep a ref to the last frame, so that we avoid setting the frame twice for no reason
  67. // _lastFrame = frame;
  68. }
  69. - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  70. // whenever the orientation changes this runs
  71. // and sets the nav bar item width to the new size width
  72. CGRect newFrame = self.frame;
  73. if (newFrame.size.width < size.width) {
  74. newFrame.size.width = size.width;
  75. newFrame.origin.x = 0;
  76. }
  77. [super setFrame:newFrame];
  78. }
  79. -(float) statusBarWidth {
  80. CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
  81. return MAX(statusBarSize.width, statusBarSize.height);
  82. }
  83. @end