swiftui-list

SwiftUI - Resizable List height that dependent on an element count

你。 提交于 2019-12-24 06:29:32
问题 I have some troubles with dynamically changing List height that dependent on elements count. I tried this solution but it didn't work. List { ForEach(searchService.searchResult, id: \.self) { item in Text(item) .font(.custom("Avenir Next Regular", size: 12)) } }.frame(height: CGFloat(searchService.searchResult.count * 20)) 回答1: TL;DR This is not how the designers of SwiftUI want you to use lists. Either you will have to come up with a hacky solution that will probably break in the future (see

Can't select same row twice in SwiftUI

自古美人都是妖i 提交于 2019-12-14 01:48:56
问题 I have a navigation list with multiple sections and rows. I select a row foo, it navigates to the view I want. However, when I go back to the root view, I can't select row foo. I tap row foo and nothing happens. I tap row bar and that row sends me to its view. Back to the root view. Then I can't select row bar, but now row foo works. Is this a bug in SwiftUI or designed behavior? Is there something I need to do to reset views when I leave them? NavigationView { List { Section(header:

SwiftUI with Xcode 11 beta 7 not updating contents of List / ForEach

夙愿已清 提交于 2019-12-13 17:12:52
问题 I've been trying a simple feature to add new entries to a List. The View will just add a new generated. item (no need for user input). struct PeopleList: View { @ObservedObject var people: PersonStore var body: some View { NavigationView { VStack { Section { Button(action: add) { Text("Add") } } Section { List { ForEach(people.people) { person in NavigationLink(destination: PersonDetail(person: person)) { PersonRow(person: person) } } } } } } .navigationBarTitle(Text("People")) .listStyle

SwiftUI show/hide sections

我是研究僧i 提交于 2019-12-13 03:55:05
问题 Im trying to build a nested layers menu, like Sketch or Photoshop. Bellow is what I did so far, using VStack with Sections to Group layers. import SwiftUI struct NestedList: View { var body: some View { ScrollView { VStack { Section (header: HStack { Image(systemName: "arrowtriangle.down") .accentColor(.black) Text("Layer Group 1") }) { HStack { Image(systemName: "arrowtriangle.right") .accentColor(.black) Text("Layer 1") } HStack { Image(systemName: "arrowtriangle.right") .accentColor(.black

Row not deleted until Edit Mode button pressed in SwiftUI Grouped Table

南笙酒味 提交于 2019-12-08 11:19:09
问题 I implemented a Grouped table in SwiftUI using an ObservableObject as the data source. A nested ForEach is used to generate each section. An EditMode() button toggles that Environment property. In Edit mode, when delete action is completed, the deleted row (unexpectedly)remains on screen. (Even though the object has been removed from the data source array.) When the user returns to normal viewing mode the object is belatedly removed from the table. In order to try to track down bug: Data

Bottom-first scrolling in SwiftUI

家住魔仙堡 提交于 2019-12-08 10:55:40
问题 How can I make a SwiftUI List start scrolling from the bottom of the screen (like a chat view)? Ideally, I want to mimic, e.g. the behavior of iMessage when the list updates, meaning it shifts down if an item is added when the user is at the bottom, but holds it’s position if the user manually scrolled up. The list is read directly from a binding array, and the order can be reversed if convenient. @komal pointed out that the UITableView (the backend of List ) has an atScrollPosition that

SwiftUI: `Toggle`s within dynamic `List` breaking their layout upon reuse?

家住魔仙堡 提交于 2019-12-08 04:46:23
问题 I'm trying to show a dynamic List with rows containing Toggle elements. The Toggle s are laid out correctly initially, but their layout breaks when scrolling them in and out of view (i. e. upon cell reuse). Minimal example code: import SwiftUI struct SwitchList: View { var body: some View { List(0..<20) { _ in SwitchRow(value: Bool.random()) } } } struct SwitchRow: View { @State var value: Bool var body: some View { Toggle(isOn: $value) { Text("A switch row") } } } Screen recording

How to dynamically create sections in a SwiftUI List/ForEach and avoid “Unable to infer complex closure return type”

那年仲夏 提交于 2019-12-06 14:37:53
I'm trying to recreate a view that matches the event list in the stock iOS Calendar app. I have the following code which generates a list of events with each event separated into its own section by Date: var body: some View { NavigationView { List { ForEach(userData.occurrences) { occurrence in Section(header: Text("\(occurrence.start, formatter: Self.dateFormatter)")) { NavigationLink( destination: OccurrenceDetail(occurrence: occurrence) .environmentObject(self.userData) ) { OccurrenceRow(occurrence: occurrence) } } } } .navigationBarTitle(Text("Events")) }.onAppear(perform: populate) } The

SwiftUI - How do I make edit rows in a list?

ⅰ亾dé卋堺 提交于 2019-12-06 10:24:10
I'd like to use the EditButton() to toggle edit mode, and have my list rows switch to edit mode. I want to include a new button in edit mode for opening a modal. I can't even get the EditMode value to switch the row content at all. struct ContentView: View { @Environment(\.editMode) var isEditMode var sampleData = ["Hello", "This is a row", "So is this"] var body: some View { NavigationView { List(sampleData, id: \.self) { rowValue in if (self.isEditMode?.value == .active) { Text("now is edit mode") // this is never displayed } else { Text(rowValue) } } .navigationBarTitle(Text("Edit A Table?"

How to bind an array and List if the array is a member of ObservableObject?

若如初见. 提交于 2019-12-04 05:49:49
问题 I want to create MyViewModel which gets data from network and then updates the arrray of results. MyView should subscribe to the $model.results and show List filled with the results. Unfortunately I get an error about "Type of expression is ambiguous without more context". How to properly use ForEach for this case? import SwiftUI import Combine class MyViewModel: ObservableObject { @Published var results: [String] = [] init() { DispatchQueue.main.asyncAfter(deadline: .now() + 1) { self