I was just trying to use GeometryReader in different places to see its effect. So looks like if there is more than one view inside, it cannot infer the result type.
import SwiftUI
struct ProgressBar: View {
static let height: Length = 8.0
@Binding var progress: CGFloat
@State var isShowing = false
var body: some View {
ZStack(alignment: .leading) {
// Unable to infer complex closure return type; add explicit type to disambiguate
GeometryReader { geometry in
//Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements
// GeometryReader { (geometry) -> View in
// Consecutive statements on a line must be separated by ';'
// GeometryReader { (geometry) -> some View in
Rectangle()
.foregroundColor(.gray)
.opacity(0.3)
.frame(width: geometry.size.width, height: Self.height)
.frame(width: geometry.size.width, height: Self.height)
Rectangle()
.foregroundColor(.blue)
.frame(width: self.isShowing ? geometry.size.width * self.progress / 100.0 : 0.0, height: Self.height)
.frame(width: self.isShowing ? geometry.size.width * self.progress / 100.0 : 0.0, height: Self.height)
.animation(.linear(duration: 0.6))
}
}
.onAppear { self.isShowing = true }
.cornerRadius(4.0)
}
}
#if DEBUG
struct ProgressBar_Previews: PreviewProvider {
static var previews: some View {
ProgressBar(progress: .constant(25.0))
}
}
#endif
See error messages inside the code at GeometryReader()
...
Documentation: Apple Developer Documentation
@frozen struct GeometryReader<Content> where Content : View
init(content: @escaping (GeometryProxy) -> Content)
So I have to somehow tell the compiler that Content
is a View
here. But I cannot figure out how to do this