Skip to content

Commit 336042c

Browse files
emily8rownmeta-codesync[bot]
authored andcommitted
Fix RCTDevLoadingView dismiss button not appearing (#54696)
Summary: Pull Request resolved: #54696 Changelog: [iOS][Fixed] - Fixed dismiss button not appearing consistently in dev loading view D87465522 introduced lazy initialization to reuse views across multiple `showMessage` calls for better performance. However, this exposed two bugs: 1. **Missing button bug**: Button creation was inside the `if (self->_container == nullptr)` block, which now only executes once. If the first call had `dismissButton=NO`, subsequent calls with `dismissButton=YES` would skip button creation since the container already existed. 2. **Button text wrapping bug**: The button didn't have compression resistance priority set, so Auto Layout could compress it to fit the layout, causing the text to wrap. This fixes both issues by: - Moving button creation/removal logic outside the container initialization so it runs on every call and dynamically adds or removes the button based on the current `dismissButton` parameter - Setting compression resistance and content hugging priorities on the button to prevent it from being compressed, forcing the message label to wrap instead - Resetting all UI elements in `hide()` to ensure clean state between loading sessions The performance optimization from D87465522 is preserved - views are still reused during rapid Metro progress updates. Reviewed By: javache Differential Revision: D87870932 fbshipit-source-id: 052d4a0685ee288e3b4bf974b203e4f29c652436
1 parent ac31e96 commit 336042c

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

packages/react-native/React/CoreModules/RCTDevLoadingView.mm

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,49 +121,61 @@ - (void)showMessage:(NSString *)message
121121
self->_label.translatesAutoresizingMaskIntoConstraints = NO;
122122
self->_label.font = [UIFont monospacedDigitSystemFontOfSize:12.0 weight:UIFontWeightRegular];
123123
self->_label.textAlignment = NSTextAlignmentCenter;
124+
[self->_container addSubview:self->_label];
125+
self->_label.numberOfLines = 0;
124126
}
125127
self->_label.textColor = color;
126128
self->_label.text = message;
127-
self->_label.numberOfLines = 0;
128129

129130
if (self->_container == nullptr) {
130131
self->_container = [[UIView alloc] init];
131132
self->_container.translatesAutoresizingMaskIntoConstraints = NO;
132133
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
133134
[self->_container addGestureRecognizer:tapGesture];
134135
self->_container.userInteractionEnabled = YES;
135-
136-
if (dismissButton) {
137-
CGFloat hue = 0.0;
138-
CGFloat saturation = 0.0;
139-
CGFloat brightness = 0.0;
140-
CGFloat alpha = 0.0;
141-
[backgroundColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
142-
UIColor *darkerBackground = [UIColor colorWithHue:hue
143-
saturation:saturation
144-
brightness:brightness * 0.7
145-
alpha:1.0];
146-
147-
UIButtonConfiguration *buttonConfig = [UIButtonConfiguration plainButtonConfiguration];
148-
buttonConfig.attributedTitle = [[NSAttributedString alloc]
149-
initWithString:@"Dismiss ✕"
150-
attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11.0 weight:UIFontWeightRegular]}];
151-
buttonConfig.contentInsets = NSDirectionalEdgeInsetsMake(6, 12, 6, 12);
152-
buttonConfig.background.backgroundColor = darkerBackground;
153-
buttonConfig.background.cornerRadius = 10;
154-
buttonConfig.baseForegroundColor = color;
155-
156-
UIAction *dismissAction = [UIAction actionWithHandler:^(__kindof UIAction *_Nonnull action) {
157-
[self hide];
158-
}];
159-
self->_dismissButton = [UIButton buttonWithConfiguration:buttonConfig primaryAction:dismissAction];
160-
self->_dismissButton.translatesAutoresizingMaskIntoConstraints = NO;
161-
[self->_container addSubview:self->_dismissButton];
162-
}
163136
[self->_container addSubview:self->_label];
164137
}
165138
self->_container.backgroundColor = backgroundColor;
166139

140+
// Handle button creation/removal dynamically based on dismissButton parameter
141+
if (dismissButton && self->_dismissButton == nullptr) {
142+
CGFloat hue = 0.0;
143+
CGFloat saturation = 0.0;
144+
CGFloat brightness = 0.0;
145+
CGFloat alpha = 0.0;
146+
[backgroundColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
147+
UIColor *darkerBackground = [UIColor colorWithHue:hue
148+
saturation:saturation
149+
brightness:brightness * 0.7
150+
alpha:1.0];
151+
152+
UIButtonConfiguration *buttonConfig = [UIButtonConfiguration plainButtonConfiguration];
153+
buttonConfig.attributedTitle = [[NSAttributedString alloc]
154+
initWithString:@"Dismiss ✕"
155+
attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11.0 weight:UIFontWeightRegular]}];
156+
buttonConfig.contentInsets = NSDirectionalEdgeInsetsMake(6, 12, 6, 12);
157+
buttonConfig.background.backgroundColor = darkerBackground;
158+
buttonConfig.background.cornerRadius = 10;
159+
buttonConfig.baseForegroundColor = color;
160+
161+
UIAction *dismissAction = [UIAction actionWithHandler:^(__kindof UIAction *_Nonnull action) {
162+
[self hide];
163+
}];
164+
self->_dismissButton = [UIButton buttonWithConfiguration:buttonConfig primaryAction:dismissAction];
165+
self->_dismissButton.translatesAutoresizingMaskIntoConstraints = NO;
166+
167+
// Prevent button from being compressed - force label to wrap instead
168+
[self->_dismissButton setContentCompressionResistancePriority:UILayoutPriorityRequired
169+
forAxis:UILayoutConstraintAxisHorizontal];
170+
[self->_dismissButton setContentHuggingPriority:UILayoutPriorityRequired
171+
forAxis:UILayoutConstraintAxisHorizontal];
172+
173+
[self->_container addSubview:self->_dismissButton];
174+
} else if (!dismissButton && self->_dismissButton != nullptr) {
175+
[self->_dismissButton removeFromSuperview];
176+
self->_dismissButton = nullptr;
177+
}
178+
167179
UIWindow *mainWindow = RCTKeyWindow();
168180
if (self->_window == nullptr) {
169181
UIWindowScene *windowScene = mainWindow.windowScene;
@@ -245,6 +257,9 @@ - (void)showMessage:(NSString *)message
245257
self->_window.frame = windowFrame;
246258
self->_window.hidden = YES;
247259
self->_window = nil;
260+
self->_container = nil;
261+
self->_label = nil;
262+
self->_dismissButton = nil;
248263
self->_hiding = false;
249264
}];
250265
});

0 commit comments

Comments
 (0)