Search code examples
iosuiviewswift4nslayoutconstraint

How can I increase performance


my app has a view that basically consists of a complex grid. There are roughly 1400 subviews. On "modern" devices the transition to this view is fairly smooth - you notice a slight delay if you are looking for it. On "older devices" such as an iPhone 6 the delay is more noticeable. It takes about a second or two for the screen to load.

I previously used UIStackViews to organise the UIViews, but that was too slow. Modern devices needed about a second and older ones more around 5 seconds. I changed my entire layout to only work with regular old layout constraints to achieve the speed mentioned above.

The views and constraints are all implemented in code - no storyboards here. I am not getting any warnings or errors. The views basically consist only of the views themselves with different backgrounds and the occasional label - nothing fancy like images or other render intensive objects.

I have also tried reducing the number of visual format NSLayoutConstraints to a bare minimum.

Is there a way for me to test which objects/views/constraints are taking the most time to render? I tried using time profiler, but was not able to detect any slow processes. Or is this basically just the baggage of so many objects?

Thanks for any tips as to where I can look - Joseph


Solution

  • Autolayout is expensive. No way you can use it for such complex views. So don't use autolayout. Don't use constraints.

    Do ALL your layout code programmatically in - (void)viewDidLayoutSubviews (remember to call super) settings frames of the views.

    If you don't need scrolling or interaction or rotation support you can consider to draw all that UI into bitmap, and show just bitmap. This is fastest way.