Search code examples
swiftswiftuics193p

Forcing a View to the bottom right of View in SwiftUI


I'm teaching myself SwiftUI by taking the 2020 version of Stanford's CS193p posted on YouTube.

I'd like to have a ZStack with a check box in the bottom right. This code puts the check mark right in the middle of the View.

ZStack {
    .... stuff ....
    Text("✓")
}

Much to my surprise, this code puts the check mark in the top left corner of the View. (My mental model of GeometryReader is clearly wrong.)

ZStack {
   .... stuff ...
   GeometryReader { _ in Text("✓") }
}

I can't use ZStack(alignment: .bottomTrailing) { ... } as suggested in a different StackOverflow answer, as I want the alignment to apply only to the text, and not to anything else.

Is there any way to get the check mark on the bottom right? I'm trying to avoid absolute offsets, since I want my code to work no matter what the size of the ZStack view. Also, is there a good tutorial on layout so that I can figure out the answer to these questions myself?

[In case you're wondering. I'm playing around with Homework Assignment 3. I'm adding a "cheat mode" where if there is a matchingSet on the board, it marks them with a small check mark.]


Solution

  • Try applying

    .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomTrailing)
    

    to Text.