Finally, the stack chooses its own size so that it exactly encloses the children. By default, the alignment is – you guessed it –. The stack lines them up with the spacing and aligns them according to the specified alignment. Whatever size it claimed, it deducts that from the unallocated space. It then proposes one of those as the size for the least flexible child. The stack divides the remaining space into equal parts for each of the remaining views. The stack figures out the internal spacing and subtracts it from the size proposed by its parent view. There are three simple steps in the stack layout process. Understanding how they work is probably more important than anything. You are going to use stacks a lot in SwiftUI. Stacks might seem almost too simple, and they are 5. The vast majority of layouts can be implemented using stacks. Stacks are the primary layout instrument in SwiftUI. Look what happens if I set the frame to 80x80. Let’s take a 60圆0 image and display it using SwiftUI’s Image. Those have nothing to do with frame(width:height:alignment:) and other related methods in SwiftUI. Let’s start with the most basic one - frames.įirst, forget everything you know about frames in UIKit or AppKit. To understand the layout system means understanding all of these instruments. This isn’t the case with SwiftUI in which everything: Stacks, Frames, Paddings, etc – is its own thing. In Auto Layout, all the APIs were built on top of the same technology - constraints. Now that we’ve looked at the most basic example and have an idea of how SwiftUI layout process works, let’s see what instruments does it offer. This is important to note, but, as far as I know, it is no different from Auto Layout. One other thing that was also mentioned on WWDC is that at the final step, SwiftUI automatically rounds the edges of your views to the nearest pixels. The system does its best to always produce the best result and give you the control when needed. There is no such thing as an ambiguous or an unsatisfiable layout 2. Unlike Auto Layout, SwiftUI always produces a valid layout. Second, as you might have noticed, we haven’t explicitly said anything about the layout, but there were no “Ambiguous Layout” warnings. This was a major source of complexity in Auto Layout and it is gone now. This wasn’t the case with Auto Layout where constraints work in both directions: in some cases, a parent would determine the size of a child, but sometimes it was the other way around. This is a major departure from Auto Layout in many important ways.įirst, it is in some ways closer to simple frame-based layout where the child had no affect on the parent’s frame. This is a simple model, but every layout in SwiftUI is calculated this way.
0 Comments
Leave a Reply. |