2

I want to know that is it possible to adjust height and width in proportion according to superview in SwiftUI to any UIControl ?

As we are doing like :

let txtFld = UITextField()
txtFld.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: self.view.frame.width/2, height: self.view.frame.height*0.1))

I know we can somehow achieve to adjust width by using Spacer(), .padding and edgeInsets. But what about height? I have seen lots of tutorial for SwiftUI, everywhere height is static. Even on Apple's site. SwiftUI Tutorials

If anyone have knowledge about how to set height in proportion with view's height, please share here. Because at some point we need like that. For ex. If it needs to make button according to device height. (Let's say in iPhone SE = 40, iPhone 8P = 55, iPhone X = 65)

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
VRAwesome
  • 4,721
  • 5
  • 27
  • 52
  • Possible duplicate of [Proportional height (or width) in SwiftUI](https://stackoverflow.com/questions/57243677/proportional-height-or-width-in-swiftui) – superpuccio Oct 08 '19 at 16:11

1 Answers1

5

You can use GeometryReader to get that information from the parent:

struct MyView: View {
    var body: some View {
        GeometryReader { geometry in
           /*
             Implement your view content here
             and you can use the geometry variable
             which contains geometry.size of the parent
             You also have function to get the bounds
             of the parent: geometry.frame(in: .global)
           */
        }
    }
}

You can have a custom struct for you View and bind it's geometry to a variable to make it's geometry accessible from out of the View itself.

- Example:

First define a view called GeometryGetter (giving credit to @kontiki):

struct GeometryGetter: View {
    @Binding var rect: CGRect

    var body: some View {
        return GeometryReader { geometry in
            self.makeView(geometry: geometry)
        }
    }

    func makeView(geometry: GeometryProxy) -> some View {
        DispatchQueue.main.async {
            self.rect = geometry.frame(in: .global)
        }

        return Rectangle().fill(Color.clear)
    }
}

Then, to get the bounds of a Text view (or any other view):

struct MyView: View {
    @State private var rect: CGRect = CGRect()

    var body: some View {
        Text("some text").background(GeometryGetter($rect))

        // You can then use rect in other places of your view:
        Rectangle().frame(width: 100, height: rect.height)
    }
}
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • 2
    @VRAwesome I have added an example. Check that out. – Mojtaba Hosseini Oct 08 '19 at 07:53
  • 1
    This looked like a fantastic idea, however for me (Xcode 11.2.1) it crashes on `self.rect = geometry.frame(in: .global)` with error "precondition failure: invalid input index: 1" – Grimxn Nov 25 '19 at 21:32
  • 1
    I haven't update my Xcode yet, I will check the issue when I'm done and let you know. I hope you can do this yourself with the help of the idea till then. ;) @Grimxn – Mojtaba Hosseini Nov 25 '19 at 21:37