使用 SwiftUI GeometryReader 而不影响外框? [英] Use SwiftUI GeometryReader without affecting outer frame?
问题描述
是否可以以不只填充父视图的方式使用 GeometryReader
?
Is it possible to use GeometryReader
in such a way that it doesn't just fill up the parent view?
作为一个具体的例子,我想使用 .padding(.bottom, geometry.safeAreaInsets.bottom > 0 ? 0 : 12.0)
控制底部边距.这将使用安全区域作为 iPhone X 上的边距和旧设备上的自定义边距.
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.
如果没有 GeometryReader
,我的视图会占用适合其实际垂直尺寸的空间.使用阅读器,视图占据屏幕的 1/2.
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)
}
推荐答案
只需将 .fixedSize()
修饰符添加到您的 HStack.这将达到预期的效果.
Just add the .fixedSize()
modifier to your HStack. That will achieve the desired affect.
这篇关于使用 SwiftUI GeometryReader 而不影响外框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!