我想知道是否可以根据height
中的超级视图将width
和SwiftUI
的比例调整为任何UIControl
?
我们正在做的事情:
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))
我知道我们可以通过使用Spacer()
,.padding
和edgeInsets
来达到调整宽度的目的。但是height
呢?我看过很多关于SwiftUI
的教程,height
都是静态的。即使在Apple网站上。 SwiftUI Tutorials
[如果有人了解如何设置与视图高度成比例的高度,请在此处分享。因为在某个时候我们需要那样。对于前。如果需要根据设备高度来制作按钮。 (假设在iPhone SE = 40,iPhone 8P = 55,iPhone X = 65)
您可以使用GeometryReader
从父母那里获得该信息:
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)
*/
}
}
}
[您可以为View
提供一个自定义结构,并将其几何图形绑定到变量,以使其可以从View
本身之外访问它的几何图形。