SwiftUI:如何在不使用几何 Reader 的情况下根据框架的大小计算视图的大小?
SwiftUI: How to calculate a view's size based on it's frame's size without using a Geometry Reader?
下面的代码效果很好,但是几何读取导致我在上游出现问题,所以我希望有相同的逻辑来基于可用的 space 构建视图的宽度,但不使用几何 Reader.可能吗?
var body: some View {
ZStack {
GeometryReader { geometry in
ZStack(alignment: .leading) {
RoundedRectangle(cornerRadius: 12.0)
.fill(Color(.lightGray))
.opacity(0.1)
.frame(height: 20)
RoundedRectangle(cornerRadius: 12.0)
.fill(getColorForBar(progress: progress))
.frame(width: getFillWidth(progress: progress, geometry: geometry), height: 20)
.animation(self.progressAnimation)
}
}
}
}
private func getFillWidth(progress: Double, geometry: GeometryProxy) -> CGFloat {
var offset: CGFloat = geometry.size.width * CGFloat(progress)
if progress == 0 {
offset = (geometry.size.width * CGFloat(progress)) + 10.0
} else if progress == 1.0 {
offset = (geometry.size.width * CGFloat(progress)) - 10.0
}
return offset
}
GeometryReader
实际上是阅读视图几何的精确工具。你只需要以不同的方式使用它(所以不影响外部布局)。
这是一个不破坏布局的解决方案。使用 Xcode 12.1 / iOS 14.1
测试
RoundedRectangle(cornerRadius: 12.0) // << this consumes available width only
.fill(Color(.lightGray))
.opacity(0.1)
.frame(height: 20)
.overlay(GeometryReader { geometry in // << this reads parent rectangle area
RoundedRectangle(cornerRadius: 12.0)
.fill(getColorForBar(progress: progress))
.frame(width: getFillWidth(progress: progress, geometry: geometry), height: 20)
.animation(self.progressAnimation)
}, alignment: .leading)
下面的代码效果很好,但是几何读取导致我在上游出现问题,所以我希望有相同的逻辑来基于可用的 space 构建视图的宽度,但不使用几何 Reader.可能吗?
var body: some View {
ZStack {
GeometryReader { geometry in
ZStack(alignment: .leading) {
RoundedRectangle(cornerRadius: 12.0)
.fill(Color(.lightGray))
.opacity(0.1)
.frame(height: 20)
RoundedRectangle(cornerRadius: 12.0)
.fill(getColorForBar(progress: progress))
.frame(width: getFillWidth(progress: progress, geometry: geometry), height: 20)
.animation(self.progressAnimation)
}
}
}
}
private func getFillWidth(progress: Double, geometry: GeometryProxy) -> CGFloat {
var offset: CGFloat = geometry.size.width * CGFloat(progress)
if progress == 0 {
offset = (geometry.size.width * CGFloat(progress)) + 10.0
} else if progress == 1.0 {
offset = (geometry.size.width * CGFloat(progress)) - 10.0
}
return offset
}
GeometryReader
实际上是阅读视图几何的精确工具。你只需要以不同的方式使用它(所以不影响外部布局)。
这是一个不破坏布局的解决方案。使用 Xcode 12.1 / iOS 14.1
测试RoundedRectangle(cornerRadius: 12.0) // << this consumes available width only
.fill(Color(.lightGray))
.opacity(0.1)
.frame(height: 20)
.overlay(GeometryReader { geometry in // << this reads parent rectangle area
RoundedRectangle(cornerRadius: 12.0)
.fill(getColorForBar(progress: progress))
.frame(width: getFillWidth(progress: progress, geometry: geometry), height: 20)
.animation(self.progressAnimation)
}, alignment: .leading)