List with two different entities based on segment selection - list

I don't know if it's possible. I would like that depending on what is selected in the segments the list changes Entity. Similar to what Apple does in its examples with Landmarks but instead of favorites ....
var body: some View {
VStack {
GeometryReader { geometry in
TabView {
NavigationView {
List {
VStack {
Picker(selection: self.$userData.tipoSeleccionado,
label: Text("....?")) {
ForEach(0..<self.Tipo.count) { index in
Text(self.Tipo[index]).tag(index)
}
} .pickerStyle(SegmentedPickerStyle())
}
if self.containedViewType == .tipo1 {
ForEach(self.listadoTipo1, id: \.self) { elemento in
NavigationLink(destination: Tipo1Detail(elementoSeleccionado: elemento)) {
Tipo1Row(tipo: elemento)
} .navigationBarTitle("Busqueda")
.navigationBarHidden(false)
} .onDelete(perform: self.removeTipo1)
}
if self.containedViewType == .tipo2 {
ForEach(self.listadoTipo2, id: \.self) { elemento in
NavigationLink(destination: Tipo2Detail(elementoSeleccionado: elemento)) {
Tipo2Row(tipo: elemento)
} .navigationBarTitle("Busqueda")
.navigationBarHidden(false)
} .onDelete(perform: self.removeTipo2)
}
} // List
}
}
}
}
}

Related

How to avoid the .searchable from appearing and disappearing?

I am having a lot of buggy behavior on .searchable, on iOS 16.1 (Xcode 14.1). As you can see in the screenshot below. When entering a view with a .searchable component it will overlap with the view in transition and then disappears.
I am trying to make the code as basic as possible.
struct ContentView: View {
var body: some View {
NavigationStack {
List {
NavigationLink {
Mailbox().navigationTitle("Inkomend")
} label: { Label("Inkomend", systemImage: "tray") }
NavigationLink {
Mailbox().navigationTitle("Verstuurd")
} label: { Label("Verstuurd", systemImage: "paperplane") }
NavigationLink {
Mailbox().navigationTitle("Prullenmand")
} label: { Label("Prullenmand", systemImage: "trash") }
}
.navigationTitle("Postbussen")
.refreshable {}
}
}
}
struct Mailbox: View {
#State private var searchQuery: String = ""
var body: some View {
List {
NavigationLink {
Text("Detail")
} label: {
VStack(alignment: .leading) {
Text("Apple").font(.headline)
Text("Verify your account.")
Text("Fijn dat je deze belangrijke stap neemt om je account te verifiëren.").lineLimit(2).foregroundColor(.secondary)
}
}
}
.searchable(text: $searchQuery)
}
}
Use NavigationView instead of NavigationStack.
Like Latin Bhuva said, the new NavigationStack is not intended to be used in this way, a NavigationView would be more correct in this case.
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink {
Mailbox().navigationTitle("Inkomend")
} label: { Label("Inkomend", systemImage: "tray") }
NavigationLink {
Mailbox().navigationTitle("Verstuurd")
} label: { Label("Verstuurd", systemImage: "paperplane") }
NavigationLink {
Mailbox().navigationTitle("Prullenmand")
} label: { Label("Prullenmand", systemImage: "trash") }
}
.navigationTitle("Postbussen")
.refreshable {}
}
}
}

Unable to use custom alignment with non-sibling views with animation

Goal: to have the tapped person in the ScrollView/HStack align it's centre with the other views using the explicit custom alignment, CentreScreenAlignment
Problem: currently nothing is happening when I tap on a given person.
Code:
extension HorizontalAlignment {
private enum CentreScreenAlignment: AlignmentID {
static func defaultValue(in d: ViewDimensions) -> CGFloat {
d[HorizontalAlignment.center]
}
}
static let centreScreenAlignment = HorizontalAlignment(CentreScreenAlignment.self)
}
struct ContentView: View {
#State private var centrePt: Int = 0
var body: some View {
GeometryReader { g in
VStack(alignment: .centreScreenAlignment) {
ZStack {
HStack {
Button("Cancel") {
// action
}
.padding(.leading)
Spacer()
Button("Done") {
// action
}
.padding(.trailing)
}
Button {
// action
} label: {
Image(systemName: "person.fill.badge.plus")
.coordinateSpace(name: "Add button")
.foregroundColor(.blue)
.alignmentGuide(.centreScreenAlignment) { d in
d[.centreScreenAlignment]
}
}
}
EmptyView()
.alignmentGuide(.centreScreenAlignment) { d in
d[.centreScreenAlignment]
}
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(1..<10) { index in
Group {
if index == self.centrePt {
Button("Patient #\(index)") {
//
}.transition(AnyTransition.identity)
.alignmentGuide(.centreScreenAlignment) { d in
d[.centreScreenAlignment]
}
} else {
Button("Person #\(index)") {
withAnimation {
centrePt = index
print(centrePt)
}
}
.transition(AnyTransition.identity)
}
}
}
}
}.padding()
Text("Hello")
}
}
}
}
Thanks very much.

ForEach Loop in Swift for Buttons

I want to use a ForEach loop to simplify the following code:
.toolbar {
ToolbarItem() {
Button {
}
label: {
Image(systemName: "magnifyingglass")
}
}
ToolbarItem() {
Button {
}
label: {
Image(systemName: "plus")
}
}
}
But it's not working. My approach only creates the "magnifyingglass" button.
My approach:
let toolbar = ["magnifyingglass", "plus"]
.toolbar {
ToolbarItem() {
ForEach(toolbar.indices) { index in
Button {
}
label: {
Image(systemName: toolbar[index])
}
}
}
}
you could try this:
struct ContentView: View {
let toolbar = ["magnifyingglass", "plus"]
var body: some View {
NavigationView {
Text("testing")
.toolbar {
ToolbarItem() {
HStack { // <--- here
ForEach(toolbar.indices) { index in
Button { }
label: { Image(systemName: toolbar[index]) }
}
}
}
}
}
}
}
import SwiftUI
struct ContentView: View {
let toolbar = ["magnifyingglass", "plus"]
var body: some View {
NavigationView {
Text("Toolbar")
.navigationTitle("")
.toolbar {
ToolbarItemGroup(placement: .navigationBarTrailing) {
ForEach(toolbar, id: \.self) { index in
Button {
} label: {
Image(systemName: ("\(index)"))
}
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I suggest (not tested) to put the for loop outside of ToolbarItem() :
let toolbar = ["magnifyingglass", "plus"]
.toolbar {
ForEach(toolbar.indices) { index in
ToolbarItem() {
Button {
}
label: {
Image(systemName: toolbar[index])
}
}
}
}

SwiftUI List with Tow Functions in Edit Mode

I try to get two functions for list rows when the view became in edit mode as shown in the photo bellow one section for select and other section for deleting, I already separate them into two section in my code any try many things but it's not work.
And this is my code, and example for a section
var body: some View {
NavigationView {
List(selection: $selection) {
if isEditMode || !groupData.rowsGroup[0].filter({$0.isOnMain}).isEmpty {
firstSection
}
if !sortedFavoriteLists.isEmpty {
favoriteSection
}
if isEditMode || !groupData.rowsGroup[1].filter({$0.isOnMain}).isEmpty {
secondSection
}
}
.listStyle(InsetGroupedListStyle())
}
}
// This is the section and each section is "ForEach" View
private var firstSection: some View {
Section {
ForEach(firstSectionData, content: MainMenuRowView.init)
.onMove { indices, newOffset in
groupData.rowsGroup[0].move(fromOffsets: indices, toOffset: newOffset)
}
}
}
One problem solved by embed everything inside "Form", but know when I activate edit mode the Lise select not appear, anyone have an answer for how to activate the selection circle in the list???
This is example code
NavigationView{
Form{
List(selection: $selection) {
ForEach(data, id: \.self) { item in
Text(item)
}.onMove(perform: { indices, newOffset in
//
})
}
Section{
ForEach(data, id: \.self) { item in
Text(item)
}
.onDelete(perform: { indexSet in
//
})
.onMove(perform: { indices, newOffset in
//
})
}
}
.navigationBarTitle("Tilte")
.navigationBarTitle("Main Menu", displayMode: .inline)
.toolbar(content: {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
})
}
And this is how its look

SwiftUI navigation from collapse list

I have collapse list in main view, i wanna to go in another view when i tap in a child of section in list, i tried but it didint work. Nothing didnt change when i tapped. What im doing wrong ?
func sectionIndex(section : Theme) -> Int {
userData.data.firstIndex(where: {$0.name == section.name})!
}
var body: some View {
NavigationView {
List {
ForEach(userData.data) { section in
Section(header: HeaderView(section: section)
.onTapGesture {
self.userData.data[self.sectionIndex(section: section)].expanded.toggle()
}, footer: EmptyView()) {
if section.expanded {
ForEach(section.questions!) { question in
QuoteView(question: question)
.onTapGesture {
NavigationLink(destination: DetailView()) {
Text("Do Something")
}
print("dsdsw")
}
}
}
}
}
}
}
}
}
struct DetailView : View {
var body: some View {
Text("Hello World B")
}
}
I don't know what is QuoteView, but try to put link into background of that view, NavigationView should handle link in it automatically, ie.
ForEach(section.questions!) { question in
QuoteView(question: question)
.background (
NavigationLink(destination: DetailView()) {
EmptyView() // << content is not needed here
}
)
}