Post

Replies

Boosts

Views

Activity

Reply to NavigationLInk
how can I create the loop to create the navigation links in body? Have you read my answer? you may need to use onAppear {...} If you cannot apply generic code example into your actual code, please include enough code in your opening post. (Please show an accurate code, no typo in it please.) import SwiftUI struct MyListView: View { let category: CategoryModel @ObservedObject var store = MyStore() // <- I guess you should use singleton for `MyStore`, but you have not shown its detail... var body: some View { List(store.categories.indices, id:\.self) { categoryIndex in let category = store.categories[categoryIndex] NavigationLink(destination: CategoryView(categoryId: category.id) ) { CategoryRow(category: category) } } .onAppear { self.store.getData(categoryId: category.id) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to NavigationLInk
MyListView is being called I do not understand what you mean by being called, but I guess you may be doing something in init or body, which may be executed at any time (which are hard to predict) by the runtime of SwiftUI. If you want to do something on appearance of a view, you may need to use onAppear {...}: (Or task {...} if you can target your app to iOS 15+.) struct MyListView: View { let category: Category var body: some View { listViewContent() .onAppear { print("onAppear called") //Do API calls here... } } @ViewBuilder private func listViewContent() -> some View { Text("listViewContent...") //... } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to Init or body ?
but need to call the method inside the body.  Can you explain why you think so? In almost all cases, you have no need to (and should not) do something in body. You may be mistaking something. Have you tried onAppear {...} ?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to Can't interpolate a Bool in SwiftUI Text?
When you write a String Interpolation directly inside Text.init(_:), it is interpreted as a LocalizedStringKey, not String. So, LocalizedStringKey.StringInterpolation works. You should better also read this: SE-0228 Fix ExpressibleByStringInterpolation If you want to make a String Interpolation of String type, you can write something like this: Text("Hello. \(test)" as String) Or Text(verbatim: "Hello. \(test)") (In both samples, "Hello. 〜" would not be localized.) To make it localizable, you may need to write: Text("Hello. \(test.description)") Or Text("Hello. \(String(test))") (To localize true or false, you may need a little more effort.)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to Autocomplete in xCode 13 RC
I tried with a vanilla SwiftUI project with the released version of Xcode 13 (same build number 13A233), and typed Picker after removing Text..., I got the expected candidates starting with the initializers of Picker. But using Xcode 13 till now, there were some other cases autocomplete did not work. So I guess Picker is not the key of this malfunctioning but your Xcode is not ready for autocomplete. One possibility is you just need to wait, or some errors in other parts of your code may affecting. Can you try with a brand-new project again?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to Collapse button NavigationView SideBar SwiftUI
If you do want SwiftUI only solution, you should not have included the tag UIKit. When the tags or the terms may not be appropriate enough, you should better try to explain your issue more precisely. Including codes and/or images will help avoiding confusion of readers. What I want to really achieve is the sidebar that is used in the iPad Settings app I could not find a simple way to hide Collapse button on navigation bar. Also, as far as I checked the UI design of Settings app of iOS 14 & 15 The UI does not change whether in portrait or in landscape Each row in the list does not have a disclosure indicator (⟩) So, I guess, the Settings app is not constructed simply using the ColumnNavigationViewStyle and NavigationLink. You may need to do it all by yourself. A little bit simplified example: struct ContentView: View { @State var secondViewTitle: String? let rows = ["Hello", "World"] var body: some View { HStack { NavigationView { List { Section { ForEach(rows, id: \.self) { row in Button(action: { if self.secondViewTitle == row { self.secondViewTitle = nil } else { self.secondViewTitle = row } }, label: { HStack { Text(row) .padding() Spacer() } }) .background(self.secondViewTitle == row ? Color.accentColor : Color.white) .listRowInsets(EdgeInsets()) } .foregroundColor(Color.primary) } } .listStyle(InsetGroupedListStyle()) .navigationTitle("Test") } .navigationViewStyle(StackNavigationViewStyle()) .frame(width: 360) SecondView(title: secondViewTitle) .frame(maxWidth: .infinity) } } } struct SecondView: View { let title: String? var body: some View { Text("\(title ?? "")") } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Sep ’21