Post

Replies

Boosts

Views

Activity

Reply to Keep ScrollView position when adding items on the top
But what about adding multiple items at the top of the list? I did that and the result was strange, when I start scrolling it couldn't scroll properly. We are developing something really important and we need that ability but Swiftui has a problem with it! struct SwiftUIView: View { @State var data: [String] = (0 ..< 25).map { String($0) } @State var dataID: String? var body: some View { ScrollView { VStack { Text("Header") LazyVStack { ForEach(data, id: \.self) { item in Color.red .frame(width: 100, height: 100) .overlay { Text("\(item)") .padding() .background() } } } .scrollTargetLayout() } } .scrollPosition(id: $dataID) .safeAreaInset(edge: .bottom) { HStack { Text("\(Text("Scrolled").bold()) \(dataIDText)") Spacer() Button { dataID = data.first } label: { Label("Top", systemImage: "arrow.up") } Button { dataID = data.last } label: { Label("Bottom", systemImage: "arrow.down") } Menu { Button("Batch Prepend") { let newDatas = (data.count ..< data.count + 6).map{"New Data \($0)"} newDatas.forEach { data in self.data.insert(data, at: 0) } // data.insert(contentsOf: newDatas, at: 0) // Does not have any difference. } Button("Prepend") { let next = "New Data 1" data.insert(next, at: 0) } Button("Append") { let next = String(data.count) data.append(next) } Button("Remove First") { data.removeFirst() } Button("Remove Last") { data.removeLast() } } label: { Label("More", systemImage: "ellipsis.circle") } } } } var dataIDText: String { dataID.map(String.init(describing:)) ?? "None" } }
Jul ’23
Reply to SwiftUI list overlap navigationBar
I fixed the problem just by removing all unnecessary views around the List and just putting the pure view at the root of the view. Also, you need to be cautious about ZStack and so on. My piece of advice is that, commenting all the views and modifiers and then step by step uncomment the views until you can find the root of the problem.
Feb ’23
Reply to Showing sheet dismiss navigation view child
I've found a solution for that but I truly believe that it's a SwiftUI bug. You must move the navigation link to the upper parent and view. import SwiftUI struct ContentView: View {     @State     var shownextPage = false     var body: some View {         NavigationView{             ZStack{                 TabView{                     Tab1(shownextPage: $shownextPage)                         .tabItem({Text("tab1")})                     Tab2()                         .tabItem({Text("tab2")})                 }                 NavigationLink(destination: SecondView(), isActive: $shownextPage) {                     EmptyView()                 }             }             .navigationViewStyle(.stack)         }     } } struct Tab1: View {     @Binding     var shownextPage:Bool     var body: some View {         ZStack{             Button ("Next Page"){                 shownextPage = true             }         }     } } struct Tab2: View {     var body: some View {         Text("Tab 2 detail")     } } struct SecondView: View {     @State     var showSheet:Bool = false     var body: some View {         VStack{             Button {                 showSheet = true             } label: {                 Text("open Sheet")                     .padding()             }         }         .sheet(isPresented: $showSheet, onDismiss: nil) {             Text("hello in sheet")         }     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } }
Nov ’21