Day Vision

Reading Everyday,Extending Vision

The pitfalls of the yoga layout: The layout scrollView crashes when updating the layout

2023-02-28 03:44:39


The problem is that scrollView is used when using yoga to lay out a sliding interface.Lay out according to the normal flow: 1.Create scrollview2.Add contentView3 to scrollView.Add content in contentView 4.Root view tuning


issue

When you use yoga to lay out a slideable interface, you use scrollview. exe. Follow the normal flow layout:

scrollView.contentSize = contentView.frame.size
6. In the controller's viewWillLayoutSubviews method, write the layout of the root view of the yoga layout again, and call applyLayoutPreservingOrigin: method again. The goal is to keep the layout correct when the screen is rotated

The problem: In Step 6, a crash occurs when you call applylayoutpreserving origin: .

The source of the problem

Bugs in the framework

When doing layout calculations, it is not correct to exclude child controls that are not part of the yoga layout. Causes the frame value to be set to {0,0, Nan, Nan } .

Crash reason

When we calculate the layout again in the viewWillLayoutSubviews method, an error occurs when setting their frame value because the horizontal/vertical slider control ScrollIndicator is hidden in the scrollView control and Yoga does not exclude these two controls when calculating the layout.(Why didn't we crash when laying out in viewDidLoad?)Because at that time, the slider control had not yet been created.lazy loading)

Workaround

at

However, this bug has not been updated in time for cocoaPod (the latest version is currently 1.18.1).