Stanford University vs Harvard University

A data analysis study comparing Stanford and Harvard University using Python

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Bridging Auto Layout with frame based layout

Have you ever run into the issue of placing your new Auto Layout based view within some older frame-based code? One problem you’ll encounter is that you need a way for the frame-based layout to know when its Auto Layout subviews change in size. It turns out that this can be solved in a pretty straight-forward way! In this post I’ll run through an example as well as the solution.

Let’s start with an example: our parent UIViewController creates an AutoViewController whose size is defined by Auto Layout constraints. Internally, our AutoViewController holds a UILabel bound to its edges. As the number of lines of text changes, the AutoViewController will expand to accommodate this.

We’ll need a way of finding the size of the AutoViewController so that we can set its frame. Using the systemLayoutSizeFitting(targetSize:) method on UIView, we can obtain the height of the subview as defined by Auto Layout. In our case, we want the view to appear as a fixed-width row within the parent, so we’ve defined a method to obtain the size. We provide the available width but ask for a compressed height — otherwise our Auto Layout view is free to stretch vertically more than it needs to, filling up the parent.

We can then use this method to set the frame of our Auto Layout based view. Though it’s not strictly required, we do this as the parent goes to lay out its subviews. Ultimately this view will be one part of a more complex frame-based layout, so if it changes, its sibling views would likely need to adjust too. viewWillLayoutSubviews() is a good place to orchestrate this.

We’re still left with a problem. Our Auto Layout based child UIViewController may change its size. Here’s what that looks like:

Thankfully, Apple gives us a way to know when this happens. By handling systemLayoutFittingSizeDidChange(forChild:) we can respond to this. Here, we mark the parent view as needing a layout pass which will do the trick, since the frames of our children are set within viewWillLayoutSubviews.

That’s it! I like this solution since it avoids having your UIView/ UIViewController classes explicitly communicate size changes to their parent — a method which is often error-prone and can lead to infinite layout passes.

Add a comment

Related posts:

What is an ETF?

The stock market can be difficult to put your money into, especially if you don’t know much about it. An easy way to get started with the stock market can be buying an ETF instead of buying…

I want to make a Rural Educational Development Society.

I have a dream to make a Rural Educational Development Society, which works for the which works for resolving of problems of rural people, which they face during getting education. The society will…

Meet Chronik Kid

The young man started making music around high achool and had always loved to write since he was a kid. According to the Chronik Kid, he started making music to, “make his soul feel at home,” and to…