I am using a LayzVStack embedded into a ScrollView. The list items are fetched from a core data store by using a @FetchResult or I tested it also with the new @Query command coming with SwiftData.
The list has one hundred items 1, 2, 3, ..., 100.
The user scrolled the ScrollView so that items 50, 51, ... 60 are visible on screen.
Now new data will be fetched from the server and updates the CoreData or SwiftData model. When I add new items to the end of the list (e.g 101, 102, 103, ...) then the ScrollView is keeping its position.
Opposite to this when I add new items to the top (0, -1, -2, -3, ...) then the ScrollView scrolls down.
Is there a way with the new SwiftData and SwiftUI ScrollView modifiers to update my list model without scrolling like with UIKit where you can query and set the scroll offset pixel wise?
Post not yet marked as solved
Hello!
I watched a great WWDC session that introduces new scroll view API in iOS 17: scrollTargetBehavior.
I'm developing a "carousel" view with new API, but got stuck with implementing a behaviour, when "inner" view, that is smaller than "edge" views, isn't centred while scrolling.
The first picture is how it works, the second one - is an expected result (so, the second card is centered when user scrolls).
Would appreciate any help or suggestions how to get it done.
PS: scrollTargetBehavior(.paging) didn't help much
~Paul
Attaching the source code:
import SwiftUI
struct CardView: View {
private let name: String
private let width: CGFloat
init(id: String, width: CGFloat) {
self.name = id
self.width = width
}
var body: some View {
Text("Card \(name)")
.frame(width: width, height: 120)
.background(Color.cyan)
.clipShape(RoundedRectangle(cornerRadius: 12))
}
}
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
let innderCardWidth = geometry.size.width * 0.7
let edgeCardWidth = geometry.size.width * 0.9
ScrollView(.horizontal) {
LazyHStack {
CardView(id: "1", width: edgeCardWidth)
CardView(id: "2", width: innderCardWidth)
CardView(id: "3", width: edgeCardWidth)
}
.scrollTargetLayout()
}
.scrollTargetBehavior(.viewAligned)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Post not yet marked as solved
scrollview did scrolling and focus view video is playing and all are video is pause. How can I implement this functionality?
I get center and top position of UIScrollView() but top video is play when focused but all are video also playing, so how can I implement only focus video is play using scrollview?
Post not yet marked as solved
Now that's a mouthful! As of iOS 17 seed 5 and continuing into seed 6, an issue was introduced where default Navigation Bar and Tab Bar behavior breaks when a View is made up of a ScrollView with another ScrollView as the safeAreaInset.
This View renders correctly using the code below, with the Navigation Bar and Tab Bar taking on a material effect when there is content behind it (screenshot 1). However, uncommenting out the ScrollView(.horizontal) so it's active causes the Nav Bar and Tab Bars to be fully transparent (screenshot 2):
TabView {
NavigationStack {
ScrollView(.vertical) {
LazyVStack {
ForEach(0...100, id: \.self) { _ in
Color(uiColor: UIColor.red)
}
}
}
.safeAreaInset(edge: .top, content: {
//ScrollView(.horizontal) {
HStack {
Color.red
Color.pink
Color.yellow
}
// }
.frame(maxWidth: .infinity)
.frame(height: 44)
})
.navigationTitle("Tab 1")
.navigationBarTitleDisplayMode(.inline)
}.tabItem { Label("One", systemImage: "bolt") }
I have tried various combinations of the new scrollLayoutTarget() and related modifiers, but I haven’t been able to find a way to have the nav and tab bars maintain their functionality if the Horizontal ScrollView is enabled. This behavior seems like it’s supposed to be supported, because it’s one of the examples featured n this year’s WWDC session “Beyond scroll views” (at 12:34).
It is also worth noting that this issue occurs whether the View is wrapped in a TabView or not. Without the TabView the problem presents the same in the Navigation Bar.
I have submitted this issue as FB12983586
Post not yet marked as solved
Will any of the changes to ScrollView announced in the Beyond Scroll Views talk be available on iOS versions pre-iOS 17 when XCode 15 is officially released? The documentation seems to say that the APIs will be iOS 17-only, but as we need to support older versions of iOS for our app, it means that we can't use any of the exciting new features (without effectively reimplementing the same on older versions of iOS). In particular, I'm interested in the new ScrollTargetBehavior API.