28

Is it possible to use GeometryReader in such a way that it doesn't just fill up the parent view?

As a specific example, I want to control a bottom margin using .padding(.bottom, geometry.safeAreaInsets.bottom > 0 ? 0 : 12.0). This would use the safe area as the margin on iPhone X and a custom margin on older devices.

Without the GeometryReader my view takes up space appropriate to its actual vertical size. With the reader the view takes up 1/2 the screen.

    VStack {
      Spacer()
      GeometryReader { proxy in // Lays out nicely without this...
        HStack {
          Text("Wrong")
          Spacer()
        }.border(Color.gray, width: 1)
      }

Incorrect LayoutDesired Layout

2 Answers 2

19

Just add the .fixedSize() modifier to your HStack. That will achieve the desired affect.

Sign up to request clarification or add additional context in comments.

1 Comment

fixedSize() works in that example, and attaching fixedSize(horizontal:vertical:) to my GeometryReader worked in the View I was actually working on (had nested ZStacks).
17

You can also try adding .aspectRatio(contentMode: .fit) modifier to GeometryReader.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.