Questions tagged [geometryreader]

A container view that defines its content as a function of its own size and coordinate space. Returns a flexible preferred size to its parent layout.

It's a component of Apple's SwiftUI language.

Learn more from Apple's documentation

184 questions
33
votes
9 answers

iOS 14 Invalid frame dimension (negative or non-finite)

My App uses GeometryReader with some padding to setup a View frame dimension inside a NavigationView. Since iOS 14 i get the following error message: Invalid frame dimension (negative or non-finite) Here is some example code to test: import…
Franco
  • 441
  • 1
  • 4
  • 3
32
votes
4 answers

SwiftUI GeometryReader does not layout custom subviews in center

I have a custom view: struct ImageContent: View { var body: some View { Image("smile") .resizable() .scaledToFit() } } Which is being placed into another view with a GeometryReader: var body: some View { …
Tom Shen
  • 1,838
  • 3
  • 19
  • 40
9
votes
3 answers

why geometry reader doesn't center its child?

red border is geometry Area and black border is text area currently using Xcode12 Beta 3 struct Testing_Geometry_: View { var body: some View { GeometryReader { geo in Text("Hello, World!") …
amin torabi
  • 281
  • 5
  • 8
9
votes
0 answers

GeometryReader is collapsing my List in SwiftUI

I wanted to use the GeometryReader to use some computation. Without my design looks like it should: var body: some View { ZStack { Color(#colorLiteral(red: 0.117543973, green: 0.2897527516, blue: 0.4028342962, alpha:…
gurehbgui
  • 14,236
  • 32
  • 106
  • 178
7
votes
6 answers

set @State var inside geometryReader

How is it possible to set a @State var inside a geometryReader? This is my code: @State var isTest:Int = 0 var body: some View { VStack{ ForEach(self.test, id: \.id) { Test in VStack{ GeometryReader {…
Stefano Vet
  • 567
  • 1
  • 7
  • 18
6
votes
1 answer

How do I correctly pass a "cell item" to a .sheet from a SwiftUI LazyVGrid?

Here is my example, and I can't tell if this is a bug or not. All my cells load correctly, but when I try to bring up the DetailView() as a sheet, the item pased in is always whatevr item is shown first in the grid (in the top left in my case here),…
Rillieux
  • 587
  • 9
  • 23
5
votes
2 answers

How to scale an image to fill the parent view without affecting the layout in SwiftUI?

Goal I often have the case where I want to scale an image so that it fills its container proportionally without modifying its frame. An example is seen in the first screenshot below. The container view has a fixed frame as indicated by the red…
Mischa
  • 15,816
  • 8
  • 59
  • 117
5
votes
3 answers

ZStack alignment not centered when in GeometryReader?

I have the following view: struct TestView: View { var body: some View { GeometryReader { geo in ZStack(alignment: Alignment(horizontal: .center, vertical: .center)) { …
zumzum
  • 17,984
  • 26
  • 111
  • 172
5
votes
1 answer

How to position views relative to other views in different coordinate systems in SwiftUI

I'm trying to implement a drag and drop functionality in my app using SwiftUI. I create two circles which are located in two different HStacks. They do not share the same coordinate space. The circle with the stroke is the target, the green filled…
Marco Boerner
  • 1,243
  • 1
  • 11
  • 34
5
votes
1 answer

SwiftUI: How to find the height of an image and use it to set the size of a frame

So I've been trying to figure out how to find the height of an image in SwiftUI for a while now with no success. Essentially I have portrait/landscape images, which the portrait ones I want to display at about 600 pt tall, whereas the landscape…
Jabinator1
  • 225
  • 4
  • 12
5
votes
1 answer

ForEach and GeometryReader: variable height for children?

I have following example: import SwiftUI struct TestSO: View { @State var cards = [ Card(title: "short title text", subtitle: "short title example"), Card(title: "medium title text text text text text", subtitle: "medium title…
lorem_stack
  • 111
  • 2
  • 11
5
votes
2 answers

How to keep Text as leading alignment after set Text().frame(maxWidth: .infinity)

It could be a simple case, but I couldn't find a solution. Here is my code, using the GeometryReader to set the relative layout of SwiftUI view size like Image. The issue in on the last VStack, I want to background of Text to go to the right end of…
Zhou Haibo
  • 1,681
  • 1
  • 12
  • 32
4
votes
1 answer

Can not get correct position of view in SwiftUI

Im trying to get midX position of Button, but it always gives me unexpected result. I have tried to use .global, .local. and .named coordinate spaces but it still don't work. Maybe there is another way to get coordinates of UI element without…
4
votes
2 answers

SwiftUI - AppStorage doesn't work with GeometryReader

Here is a simple example. You can create new SwiftUI iOS project and copy it to ContentView file. import SwiftUI struct Settings { static let onOff = "onOff" } struct ContentView: View { @AppStorage(wrappedValue: false, Settings.onOff) var…
mallow
  • 2,368
  • 2
  • 22
  • 63
4
votes
2 answers

SwiftUI - Calculate height for long GeometryReader inside ScrollView

Based off of this question: SwiftUI - Drawing (curved) paths between views I draw the needed views and paths using the GeometryReader. The problem is, now it becomes too long for the screen to show it all (there are 15 such views but you can only…
Big_Chair
  • 2,781
  • 3
  • 31
  • 58
1
2 3
12 13