Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions ios/RCCCustomTitleView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,16 +60,56 @@ - (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<UIViewControllerTransitionCoordinator>)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;
}
[super setFrame:newFrame];
}

-(float) statusBarWidth {
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
return MAX(statusBarSize.width, statusBarSize.height);
}


@end