-
-
Notifications
You must be signed in to change notification settings - Fork 34
Open
Description
If we place ScrollView
inside HStack or VStack, it takes all remaining space. To fit ScrollView to its content, we need to get its content size and constrain ScrollView size.
Use a GeometryReader
as Scrollview content background, and get the local frame
import SwiftUI
struct HSearchBar: View {
@State
private var scrollViewContentSize: CGSize = .zero
var body: some View {
HStack {
searchButton
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 12) {
ForEach(store.collections) { collection in
collectionCell(collection)
}
}
.background(
GeometryReader { geo -> Color in
DispatchQueue.main.async {
scrollViewContentSize = geo.size
}
return Color.clear
}
)
}
.frame(
maxWidth: scrollViewContentSize.width
)
}
}
}
onmyway133, MaxWernersen99, lutluthfi, ThomasAWRaku, teonicel and 19 moreonmyway133, sidkamaria1337, lutluthfi, colejd, daliad007 and 6 moreonmyway133, lutluthfi, Matejkob, daliad007, slxl and 3 more