Skip to content

Commit 19c7e4e

Browse files
committed
fix for mis-alignment on iOS 26
1 parent a25439a commit 19c7e4e

4 files changed

Lines changed: 48 additions & 13 deletions

File tree

ios/RNNReactTitleView.mm

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#import "RNNReactTitleView.h"
22

3+
static const CGFloat kTitleViewDefaultHeight = 44.0;
4+
35
@implementation RNNReactTitleView {
46
BOOL _fillParent;
57
CGFloat _expectedHeight;
@@ -11,19 +13,30 @@ - (NSString *)componentType {
1113

1214
- (CGSize)intrinsicContentSize {
1315
if (_fillParent) {
14-
return CGSizeMake(UILayoutFittingExpandedSize.width, _expectedHeight > 0 ? _expectedHeight : 44);
15-
} else {
16-
return [super intrinsicContentSize];
16+
return CGSizeMake(UILayoutFittingExpandedSize.width,
17+
_expectedHeight > 0 ? _expectedHeight : kTitleViewDefaultHeight);
1718
}
19+
return [super intrinsicContentSize];
20+
}
21+
22+
- (CGSize)sizeThatFits:(CGSize)size {
23+
if (_fillParent) {
24+
return size;
25+
}
26+
return [super sizeThatFits:size];
1827
}
1928

2029
- (void)setAlignment:(NSString *)alignment inFrame:(CGRect)frame {
2130
if ([alignment isEqualToString:@"fill"]) {
2231
_fillParent = YES;
23-
_expectedHeight = frame.size.height;
24-
self.translatesAutoresizingMaskIntoConstraints = NO;
32+
_expectedHeight = frame.size.height > 0 ? frame.size.height : kTitleViewDefaultHeight;
33+
self.frame = frame;
34+
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin |
35+
UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
2536
self.sizeFlexibility = RCTRootViewSizeFlexibilityNone;
2637
} else {
38+
_fillParent = NO;
39+
self.autoresizingMask = UIViewAutoresizingNone;
2740
self.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
2841
__weak RNNReactView *weakSelf = self;
2942
[self setRootViewDidChangeIntrinsicSize:^(CGSize intrinsicSize) {
@@ -32,6 +45,14 @@ - (void)setAlignment:(NSString *)alignment inFrame:(CGRect)frame {
3245
}
3346
}
3447

48+
- (void)layoutSubviews {
49+
[super layoutSubviews];
50+
if (_fillParent && self.bounds.size.height > 0 && _expectedHeight != self.bounds.size.height) {
51+
_expectedHeight = self.bounds.size.height;
52+
[self invalidateIntrinsicContentSize];
53+
}
54+
}
55+
3556
- (void)setRootViewDidChangeIntrinsicSize:(void (^)(CGSize))rootViewDidChangeIntrinsicSize {
3657
_rootViewDidChangeIntrinsicSize = rootViewDidChangeIntrinsicSize;
3758
self.delegate = self;

ios/TopBarTitlePresenter.mm

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,26 @@ - (void)setCustomNavigationTitleView:(RNNTopBarOptions *)options
8383
reactViewReadyBlock:readyBlock];
8484
_customTitleView.backgroundColor = UIColor.clearColor;
8585
NSString *alignment = [options.title.component.alignment withDefault:@""];
86-
[_customTitleView setAlignment:alignment
87-
inFrame:viewController.navigationController.navigationBar.frame];
86+
UINavigationBar *navigationBar = viewController.navigationController.navigationBar;
87+
CGRect barBounds = navigationBar.bounds;
88+
[_customTitleView setAlignment:alignment inFrame:barBounds];
8889
[_customTitleView layoutIfNeeded];
8990

9091
viewController.navigationItem.titleView = nil;
9192
viewController.navigationItem.titleView = _customTitleView;
93+
94+
__weak RNNReactTitleView *weakTitleView = _customTitleView;
95+
__weak UIViewController *weakViewController = viewController;
96+
dispatch_async(dispatch_get_main_queue(), ^{
97+
UINavigationController *navigationController = weakViewController.navigationController;
98+
if (!navigationController || !weakTitleView) {
99+
return;
100+
}
101+
[weakTitleView setAlignment:alignment inFrame:navigationController.navigationBar.bounds];
102+
weakViewController.navigationItem.titleView = weakTitleView;
103+
[weakTitleView setNeedsLayout];
104+
[weakTitleView layoutIfNeeded];
105+
});
92106
[_customTitleView componentWillAppear];
93107
[_customTitleView componentDidAppear];
94108
} else {

playground/src/screens/CustomTopBar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ export default class CustomTopBar extends React.Component<Props> {
2525

2626
const styles = StyleSheet.create({
2727
container: {
28-
alignSelf: 'baseline',
28+
flex: 1,
29+
justifyContent: 'center',
2930
},
3031
text: {
31-
alignSelf: 'flex-start',
3232
color: 'black',
3333
fontSize: 16,
3434
},

playground/src/screens/TopBarTitleTestScreen.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const {
1818
// TopBar title component WITH subtitle
1919
function TopBarWithSubtitle() {
2020
return (
21-
<View style={{ flex: 1 }}>
22-
<View style={{ flexDirection: 'row' }}>
21+
<View style={{ flex: 1, justifyContent: 'center' }}>
22+
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
2323
<View
2424
testID={TOPBAR_TITLE_AVATAR}
2525
style={{ alignSelf: 'center', marginRight: 20, width: 10, height: 10, backgroundColor: 'red' }}
@@ -38,8 +38,8 @@ function TopBarWithSubtitle() {
3838
// TopBar title component WITHOUT subtitle - this triggers the bug on Android
3939
function TopBarWithoutSubtitle() {
4040
return (
41-
<View style={{ flex: 1 }}>
42-
<View style={{ flexDirection: 'row' }}>
41+
<View style={{ flex: 1, justifyContent: 'center' }}>
42+
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
4343
<View
4444
testID={TOPBAR_TITLE_AVATAR}
4545
style={{ alignSelf: 'center', marginRight: 20, width: 10, height: 10, backgroundColor: 'red' }}

0 commit comments

Comments
 (0)