@@ -151,6 +151,9 @@ - (UIAccessibilityTraits)accessibilityTraits {
151151
152152@interface FlutterScrollableSemanticsObject ()
153153@property (nonatomic ) FlutterSemanticsScrollView* scrollView;
154+ - (UIView*)reattachScrollViewToCurrentBridgeView ;
155+ - (void )updateScrollViewForCurrentBridgeView ;
156+ - (void )updateScrollViewGeometry ;
154157@end
155158
156159@implementation FlutterScrollableSemanticsObject
@@ -164,10 +167,7 @@ - (instancetype)initWithBridge:(fml::WeakPtr<flutter::AccessibilityBridgeIos>)br
164167 [_scrollView setShowsVerticalScrollIndicator: NO ];
165168 [_scrollView setContentInset: UIEdgeInsetsZero];
166169 [_scrollView setContentInsetAdjustmentBehavior: UIScrollViewContentInsetAdjustmentNever];
167- UIView* containerView = self.bridgeView ;
168- if (containerView) {
169- [containerView addSubview: _scrollView];
170- }
170+ [self reattachScrollViewToCurrentBridgeView ];
171171 }
172172 return self;
173173}
@@ -177,6 +177,36 @@ - (void)dealloc {
177177}
178178
179179- (void )accessibilityBridgeDidFinishUpdate {
180+ [self updateScrollViewForCurrentBridgeView ];
181+ }
182+
183+ - (void )accessibilityBridgeDidChangeView {
184+ [self updateScrollViewForCurrentBridgeView ];
185+ }
186+
187+ - (id )nativeAccessibility {
188+ return _scrollView;
189+ }
190+
191+ // private methods
192+
193+ - (UIView*)reattachScrollViewToCurrentBridgeView {
194+ UIView* view = self.bridgeView ;
195+ if (_scrollView.superview != view) {
196+ [_scrollView removeFromSuperview ];
197+ [view addSubview: _scrollView];
198+ }
199+ return view;
200+ }
201+
202+ - (void )updateScrollViewForCurrentBridgeView {
203+ UIView* view = [self reattachScrollViewToCurrentBridgeView ];
204+ if (view) {
205+ [self updateScrollViewGeometry ];
206+ }
207+ }
208+
209+ - (void )updateScrollViewGeometry {
180210 // In order to make iOS think this UIScrollView is scrollable, the following
181211 // requirements must be true.
182212 // 1. contentSize must be bigger than the frame size.
@@ -185,20 +215,14 @@ - (void)accessibilityBridgeDidFinishUpdate {
185215 // Once the requirements are met, the iOS uses contentOffset to determine
186216 // what scroll actions are available. e.g. If the view scrolls vertically and
187217 // contentOffset is 0.0, only the scroll down action is available.
188- self. scrollView .frame = self.accessibilityFrame ;
189- self. scrollView .contentSize = [self contentSizeInternal ];
218+ _scrollView .frame = self.accessibilityFrame ;
219+ _scrollView .contentSize = [self contentSizeInternal ];
190220 // See the documentation on `isDoingSystemScrolling`.
191- if (!self. scrollView .isDoingSystemScrolling ) {
192- [self .scrollView setContentOffset: self .contentOffsetInternal animated: NO ];
221+ if (!_scrollView .isDoingSystemScrolling ) {
222+ [_scrollView setContentOffset: self .contentOffsetInternal animated: NO ];
193223 }
194224}
195225
196- - (id )nativeAccessibility {
197- return self.scrollView ;
198- }
199-
200- // private methods
201-
202226- (float )scrollExtentMax {
203227 if (!self.bridge ) {
204228 return 0 .0f ;
@@ -240,7 +264,7 @@ - (CGSize)contentSizeInternal {
240264
241265- (CGPoint)contentOffsetInternal {
242266 CGPoint result;
243- CGPoint origin = self. scrollView .frame .origin ;
267+ CGPoint origin = _scrollView .frame .origin ;
244268 const SkRect& rect = self.node .rect ;
245269 if (self.node .actions & flutter::kVerticalScrollSemanticsActions ) {
246270 result = ConvertPointToGlobal (self, CGPointMake (rect.x (), rect.y () + [self scrollPosition ]));
@@ -278,11 +302,17 @@ - (instancetype)initWithBridge:(fml::WeakPtr<flutter::AccessibilityBridgeIos>)br
278302 uid : (int32_t )uid {
279303 FML_DCHECK (bridge) << " bridge must be set" ;
280304 FML_DCHECK (uid >= kRootNodeId );
305+ flutter::AccessibilityBridgeIos* accessibility_bridge = bridge.get ();
306+ if (!accessibility_bridge) {
307+ return nil ;
308+ }
281309 // Initialize with the UIView as the container.
282310 // The UIView will not necessarily be accessibility parent for this object.
283311 // The bridge informs the OS of the actual structure via
284312 // `accessibilityContainer` and `accessibilityElementAtIndex`.
285- self = [super initWithAccessibilityContainer: bridge ? bridge->view () : nil ];
313+ UIView* accessibilityContainer =
314+ accessibility_bridge->AccessibilityElementInitializationContainer ();
315+ self = [super initWithAccessibilityContainer: accessibilityContainer];
286316
287317 if (self) {
288318 _weakBridge = bridge;
@@ -346,7 +376,7 @@ - (BOOL)hasChildren {
346376
347377- (UIView*)bridgeView {
348378 flutter::AccessibilityBridgeIos* bridge = [self bridge ];
349- return bridge ? bridge->view () : nil ;
379+ return bridge ? bridge->ViewIfLoaded () : nil ;
350380}
351381
352382- (void )setSemanticsNode : (const flutter::SemanticsNode*)node {
@@ -356,6 +386,9 @@ - (void)setSemanticsNode:(const flutter::SemanticsNode*)node {
356386- (void )accessibilityBridgeDidFinishUpdate { /* Do nothing by default */
357387}
358388
389+ - (void )accessibilityBridgeDidChangeView { /* Do nothing by default */
390+ }
391+
359392/* *
360393 * Whether calling `setSemanticsNode:` with `node` would cause a layout change.
361394 */
@@ -925,11 +958,17 @@ @implementation SemanticsObjectContainer {
925958
926959- (instancetype )initWithSemanticsObject : (SemanticsObject*)semanticsObject {
927960 FML_DCHECK (semanticsObject) << " semanticsObject must be set" ;
961+ flutter::AccessibilityBridgeIos* accessibility_bridge = semanticsObject.bridge ;
962+ if (!accessibility_bridge) {
963+ return nil ;
964+ }
928965 // Initialize with the UIView as the container.
929966 // The UIView will not necessarily be accessibility parent for this object.
930967 // The bridge informs the OS of the actual structure via
931968 // `accessibilityContainer` and `accessibilityElementAtIndex`.
932- self = [super initWithAccessibilityContainer: semanticsObject.bridgeView];
969+ UIView* accessibilityContainer =
970+ accessibility_bridge->AccessibilityElementInitializationContainer ();
971+ self = [super initWithAccessibilityContainer: accessibilityContainer];
933972
934973 if (self) {
935974 _semanticsObject = semanticsObject;
0 commit comments