diff --git a/ios/RCCCustomTitleView.m b/ios/RCCCustomTitleView.m index d3f987b7b55..3d6ceda14fe 100644 --- a/ios/RCCCustomTitleView.m +++ b/ios/RCCCustomTitleView.m @@ -12,13 +12,14 @@ @interface RCCCustomTitleView () @property (nonatomic, strong) RCTRootView *subView; @property (nonatomic, strong) NSString *alignment; - +@property float initialWidth; @end @implementation RCCCustomTitleView - (instancetype)initWithFrame:(CGRect)frame subView:(RCTRootView*)subView alignment:(NSString*)alignment { self = [super init]; + _initialWidth = frame.size.width; if (self) { self.subView = subView; @@ -59,11 +60,45 @@ - (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView { } } +- (void)setFrame:(CGRect) frame { + float referenceWidth = [self statusBarWidth]; + if (referenceWidth == 0) { + referenceWidth = _initialWidth; + } + float newNavBarWidth = frame.size.width; + BOOL frameNeedsToBeCorrected = newNavBarWidth < referenceWidth || CGRectEqualToRect(self.frame, CGRectZero); + + if (frameNeedsToBeCorrected) { + // first we need to find out the total point diff of the status bar and the nav bar + float navBarHorizontalMargin = referenceWidth - newNavBarWidth; + + CGRect correctedFrame = frame; + + // then we need to place the nav bar half times the horizontal margin to the left + correctedFrame.origin.x = -(navBarHorizontalMargin / 2); + + // and finally set the width so that it's equal to the status bar width + correctedFrame.size.width = referenceWidth; + + [super setFrame:correctedFrame]; + } else if (frame.size.height != self.frame.size.height) { // otherwise + // if only the height has changed + CGRect newHeightFrame = self.frame; + // make sure we update just the height + newHeightFrame.size.height = frame.size.height; + [super setFrame:newHeightFrame]; + } + + // keep a ref to the last frame, so that we avoid setting the frame twice for no reason +// _lastFrame = frame; +} + + - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { // whenever the orientation changes this runs // and sets the nav bar item width to the new size width CGRect newFrame = self.frame; - + if (newFrame.size.width < size.width) { newFrame.size.width = size.width; newFrame.origin.x = 0; @@ -71,4 +106,10 @@ - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id