From 0d046b0dd6e83801fe8ab870a6899c9002222f98 Mon Sep 17 00:00:00 2001 From: Richard Barnes Date: Thu, 3 Dec 2026 23:56:12 -0800 Subject: [PATCH] Register for trait changes instead of overriding -traitCollectionDidChange: on iOS 17+ Summary: UIKit deprecated `-traitCollectionDidChange:` in iOS 17 in favor of `-registerForTraitChanges:withAction:`. Apps with a deployment target of iOS 17 or later warn about the override with `-Wdeprecated-implementations`. `RCTSurfaceHostingView`, `RCTTextInputComponentView`, `RCTViewComponentView` and `RCTView` now register, on iOS and tvOS 17 and later, for the traits their override checked, with an action that holds the old body. The registration runs in each class's initializer. `RCTTextInputComponentView` compares against `self.traitCollection` rather than the global current trait collection, which UIKit sets only during `-traitCollectionDidChange:`. The override is kept only while the deployment target is below iOS 17; there it returns early on iOS 17 and later and otherwise calls the same action. Behavior per OS: - Before iOS 17: unchanged. - iOS and tvOS 17 and later: the same code runs from the trait registration, for changes to the traits it checks. - Builds with a deployment target of iOS 17 or later no longer contain the deprecated override. Changelog: [Internal] Differential Revision: D133273358 --- .../RCTSurfaceHostingView.mm | 15 +++++++++++++++ .../TextInput/RCTTextInputComponentView.mm | 18 ++++++++++++++++-- .../View/RCTViewComponentView.mm | 15 +++++++++++++++ packages/react-native/React/Views/RCTView.m | 15 +++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/packages/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingView.mm b/packages/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingView.mm index 0bd67027bc08..1050714ae12f 100644 --- a/packages/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingView.mm +++ b/packages/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingView.mm @@ -37,6 +37,10 @@ - (instancetype)initWithSurface:(id)surface sizeMeasureMode:(RCTSurfaceSizeMeasureMode)sizeMeasureMode { if (self = [super initWithFrame:CGRectZero]) { + if (@available(iOS 17.0, tvOS 17.0, *)) { + [self registerForTraitChanges:@[ UITraitUserInterfaceStyle.class ] + withAction:@selector(_RCTSurfaceHostingView_traitsDidChange:previousTraitCollection:)]; + } _surface = surface; _sizeMeasureMode = sizeMeasureMode; _autoHideDisabled = NO; @@ -199,10 +203,21 @@ - (void)setActivityIndicatorViewFactory:(RCTSurfaceHostingViewActivityIndicatorV #pragma mark - UITraitCollection updates +#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_17_0 +// Before iOS 17 only; on iOS 17 and later -registerForTraitChanges:withAction: (set up in the initializer) replaces it. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { [super traitCollectionDidChange:previousTraitCollection]; + if (@available(iOS 17.0, tvOS 17.0, *)) { + return; + } + [self _RCTSurfaceHostingView_traitsDidChange:self previousTraitCollection:previousTraitCollection]; +} +#endif +- (void)_RCTSurfaceHostingView_traitsDidChange:(id)traitEnvironment + previousTraitCollection:(UITraitCollection *)previousTraitCollection +{ if (RCTSharedApplication().applicationState == UIApplicationStateBackground) { return; } diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm index 1abff21ef14e..004cac8c8efb 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm @@ -102,6 +102,10 @@ @implementation RCTTextInputComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { + if (@available(iOS 17.0, tvOS 17.0, *)) { + [self registerForTraitChanges:@[ UITraitPreferredContentSizeCategory.class ] + withAction:@selector(_RCTTextInputComponentView_traitsDidChange:previousTraitCollection:)]; + } const auto &defaultProps = TextInputShadowNode::defaultSharedProps(); _props = defaultProps; @@ -150,12 +154,22 @@ - (void)didMoveToWindow } // TODO: replace with registerForTraitChanges once iOS 17.0 is the lowest supported version +#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_17_0 +// Before iOS 17 only; on iOS 17 and later -registerForTraitChanges:withAction: (set up in the initializer) replaces it. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { [super traitCollectionDidChange:previousTraitCollection]; + if (@available(iOS 17.0, tvOS 17.0, *)) { + return; + } + [self _RCTTextInputComponentView_traitsDidChange:self previousTraitCollection:previousTraitCollection]; +} +#endif - if (UITraitCollection.currentTraitCollection.preferredContentSizeCategory != - previousTraitCollection.preferredContentSizeCategory) { +- (void)_RCTTextInputComponentView_traitsDidChange:(id)traitEnvironment + previousTraitCollection:(UITraitCollection *)previousTraitCollection +{ + if (self.traitCollection.preferredContentSizeCategory != previousTraitCollection.preferredContentSizeCategory) { const auto &newTextInputProps = static_cast(*_props); _backedTextInputView.defaultTextAttributes = RCTNSTextAttributesFromTextAttributes(newTextInputProps.getEffectiveTextAttributes(RCTFontSizeMultiplier())); diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm index 200dd1e14e73..853babe084bc 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -136,6 +136,10 @@ + (void)load - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { + if (@available(iOS 17.0, tvOS 17.0, *)) { + [self registerForTraitChanges:UITraitCollection.systemTraitsAffectingColorAppearance + withAction:@selector(_RCTViewComponentView_traitsDidChange:previousTraitCollection:)]; + } _props = ViewShadowNode::defaultSharedProps(); _reactSubviews = [NSMutableArray new]; #if !TARGET_OS_TV @@ -202,10 +206,21 @@ - (void)setBackgroundColor:(UIColor *)backgroundColor _backgroundColor = backgroundColor; } +#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_17_0 +// Before iOS 17 only; on iOS 17 and later -registerForTraitChanges:withAction: (set up in the initializer) replaces it. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { [super traitCollectionDidChange:previousTraitCollection]; + if (@available(iOS 17.0, tvOS 17.0, *)) { + return; + } + [self _RCTViewComponentView_traitsDidChange:self previousTraitCollection:previousTraitCollection]; +} +#endif +- (void)_RCTViewComponentView_traitsDidChange:(id)traitEnvironment + previousTraitCollection:(UITraitCollection *)previousTraitCollection +{ if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) { [self invalidateLayer]; } diff --git a/packages/react-native/React/Views/RCTView.m b/packages/react-native/React/Views/RCTView.m index bc39a7212f7b..f1868dc2cdd8 100644 --- a/packages/react-native/React/Views/RCTView.m +++ b/packages/react-native/React/Views/RCTView.m @@ -122,6 +122,10 @@ @implementation RCTView { - (instancetype)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { + if (@available(iOS 17.0, tvOS 17.0, *)) { + [self registerForTraitChanges:UITraitCollection.systemTraitsAffectingColorAppearance + withAction:@selector(_RCTView_traitsDidChange:previousTraitCollection:)]; + } _borderWidth = -1; _borderTopWidth = -1; _borderRightWidth = -1; @@ -616,10 +620,21 @@ - (void)layoutSubviews } } +#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_17_0 +// Before iOS 17 only; on iOS 17 and later -registerForTraitChanges:withAction: (set up in the initializer) replaces it. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { [super traitCollectionDidChange:previousTraitCollection]; + if (@available(iOS 17.0, tvOS 17.0, *)) { + return; + } + [self _RCTView_traitsDidChange:self previousTraitCollection:previousTraitCollection]; +} +#endif +- (void)_RCTView_traitsDidChange:(id)traitEnvironment + previousTraitCollection:(UITraitCollection *)previousTraitCollection +{ if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) { [self.layer setNeedsDisplay]; }