如何在SwiftUI中与superview成比例地设置高度和宽度? [英] How to set height and width in proportion with superview in SwiftUI?

查看:55
本文介绍了如何在SwiftUI中与superview成比例地设置高度和宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以根据SwiftUI中的superview将heightwidth按比例调整到任何UIControl ?

正如我们正在做的那样:

让 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))

我知道我们可以通过使用 Spacer().paddingedgeInsets 以某种方式实现调整宽度.但是 height 呢?我看过很多关于 SwiftUI 的教程,到处都是 height 是静态的.甚至在苹果的网站上.SwiftUI 教程

如果有人知道如何设置与视图高度成比例的高度,请在这里分享.因为在某些时候我们需要这样.例如.如果需要根据设备高度制作按钮.(假设 iPhone SE = 40、iPhone 8P = 55、iPhone X = 65)

解决方案

您可以使用 GeometryReader 从父级获取该信息:

struct MyView:查看{var主体:一些视图{GeometryReader { 几何输入/*在此处实现您的视图内容你可以使用几何变量其中包含父级的 geometry.size您还具有获取边界的功能父级的:geo​​metry.frame(in: .global)*/}}}

您可以为您自定义结构View,并将其几何图形绑定到一个变量,使其几何图形可从View 本身之外访问.

- 示例:

首先定义一个名为 GeometryGetter 的视图(感谢@kontiki):

struct GeometryGetter:查看{@Binding var rect:CGRectvar主体:一些视图{返回 GeometryReader { 几何输入self.makeView(几何:几何)}}func makeView(geometry: GeometryProxy) ->一些视图{DispatchQueue.main.async {self.rect = geometry.frame(in: .global)}返回矩形().填充(颜色.清除)}}

然后,获取文本视图(或任何其他视图)的边界:

struct MyView: View {@State 私有变量 rect: CGRect = CGRect()var主体:一些视图{文本(一些文本").背景(GeometryGetter($rect))//然后您可以在视图的其他位置使用 rect:Rectangle().frame(width: 100, height: rect.height)}}

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)

解决方案

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)
    }
}

这篇关于如何在SwiftUI中与superview成比例地设置高度和宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆