I am building a complex View with a number of different TextFields. I've shown a simplified version below- the first code block builds and runs. When I add an identical VStack block to it- the second code block, it fails to run- I get "the compiler is unable to type-check" error. Is the a child limit issue? I wish XCode provided decent error messaging!
Working.....
var body: some View {
ScrollView() {
VStack{ // parts
ForEach(Array(stride(from: 0, to: 461, by: 33)), id: \.self) { index_v in
HStack {
VStack {
Group{
Text("resultsLine1")
Text(PartDisplay[index_v][0])
Text(db.sPart[index_v][1])
if (db.sSelectedMode == "detailMode") {
Text("resultsLine4D")
Text("\(db.sPart[index_v][16]) \(db.sPart[index_v][19]) \(db.sPart[index_v][18])")
Text("results")
Text("\(db.sPart[index_v][22])")
} else {
Text("resultsLine4B")
Text("\(db.sPart[index_v][16]) \(db.sPart[index_v][18])")
}
}
}
.padding(.bottom, 6)
VStack {
Group{
Text("resultsLine4")
Text(PartDisplay[index_v][2])
Text(db.sPart[index_v][3])
Text(db.sPart[index_v][4])
if (db.sSelectedMode == "detailMode") {
Text("resultsLine4D")
Text("\(db.sPart[index_v][1]) \(db.sPart[index_v][3]) \(db.sPart[index_v][8])")
Text("results4")
Text("\(db.sPart[index_v][22])")
} else {
Text("resultsLine4B")
Text("\(db.sPart[index_v][16]) \(db.sPart[index_v][18])")
}
}
}
.padding(.bottom, 6)
}
}
}
}
}
Not working..
var body: some View {
ScrollView() {
VStack{ // parts
ForEach(Array(stride(from: 0, to: 461, by: 33)), id: \.self) { index_v in
HStack {
VStack {
Group{
Text("resultsLine1")
Text(PartDisplay[index_v][0])
Text(db.sPart[index_v][1])
if (db.sSelectedMode == "detailMode") {
Text("resultsLine4D")
Text("\(db.sPart[index_v][16]) \(db.sPart[index_v][19]) \(db.sPart[index_v][18])")
Text("results")
Text("\(db.sPart[index_v][22])")
} else {
Text("resultsLine4B")
Text("\(db.sPart[index_v][16]) \(db.sPart[index_v][18])")
}
}
}
.padding(.bottom, 6)
VStack {
Group{
Text("resultsLine4")
Text(PartDisplay[index_v][2])
Text(db.sPart[index_v][3])
Text(db.sPart[index_v][4])
if (db.sSelectedMode == "detailMode") {
Text("resultsLine4D")
Text("\(db.sPart[index_v][1]) \(db.sPart[index_v][3]) \(db.sPart[index_v][8])")
Text("results4")
Text("\(db.sPart[index_v][22])")
} else {
Text("resultsLine4B")
Text("\(db.sPart[index_v][16]) \(db.sPart[index_v][18])")
}
}
}
.padding(.bottom, 6)
VStack {
Group{
Text("resultsLine1")
Text(PartDisplay[index_v][0])
Text(db.sPart[index_v][1])
if (db.sSelectedMode == "detailMode") {
Text("resultsLine14D")
Text("\(db.sPart[index_v][6]) \(db.sPart[index_v][9]) \(db.sPart[index_v][8])")
Text("results")
Text("\(db.sPart[index_v][2])")
} else {
Text("resultsLine4B")
Text("\(db.sPart[index_v][6]) \(db.sPart[index_v][18])")
}
}
}
.padding(.bottom, 6)
}
}
}
}
}
Related
Any idea why the Text and NavigationLink views aren’t aligned to the left?
struct SettingsView: View {
var body: some View {
NavigationView {
ZStack {
Color.white
.edgesIgnoringSafeArea(.all)
ScrollView {
VStack {
AccountActionsView()
}
.navigationBarTitle(Text("Settings"), displayMode: .inline)
}
}
}
}
}
struct AccountActionsView: View {
var body: some View {
ZStack {
CardView()
.foregroundColor(.black)
VStack(alignment: .leading, spacing: 8) {
Text("Account actions")
.font(.custom("ArialRoundedMTBold", size: 16))
.padding(.bottom)
NavigationLink("Account control") {
Form {
Button("Log out") {
}
Button("Delete account") {
}.foregroundColor(.red)
}
.font(.custom("ArialRoundedMTBold", size: 14))
.navigationBarTitle(Text("Account control"), displayMode: .inline)
}
NavigationLink("Blocked users list") {
}
}
.font(.custom("ArialRoundedMTBold", size: 14))
}
}
}
Above is a list cell with .disabled(amount == 100) modifier
i want the same disabled list cell with original foreground color like below image
can anyone help me out Thanks in advance
var body: some View {
VStack {
Divider()
.frame(maxWidth: .infinity)
List {
Section(header: Text("Items")) {
ForEach(paymentViewModel.listOfItems) { item in
ZStack{
NavigationLink(destination: getDestination(item: item)) {
if #available(iOS 15.0, *) {
PaymentOverviewCell(item: item)
.padding(.top, 8)
} else {
PaymentOverViewCell2(item: item)
}
}
}
.deleteDisabled(item.type == .service)
.disabled(item.type == .service) // here service should not be faded out
}
.onDelete(perform: paymentViewModel.removeItemFromList)
.listRowBackground(Color.clear)
}
}
.listStyle(.inset)
Spacer()
footer
.padding()
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Payment")
.navigationBarItems(leading: btnBack)
.navigationBarBackButtonHidden(true)
}
i found one answer
var body: some View {
VStack {
Divider()
.frame(maxWidth: .infinity)
List {
Section(header: Text("Items")) {
ForEach(paymentViewModel.listOfItems) { item in
ZStack{
// if service then don't embed in navigation link
if item.type == .service {
PaymentOverViewCell2(item: item)
.padding(.top, 8)
} else {
NavigationLink(destination: getDestination(item: item)) {
if #available(iOS 15.0, *) {
PaymentOverviewCell(item: item)
.padding(.top, 8)
} else {
PaymentOverViewCell2(item: item)
}
}
}
}
.deleteDisabled(item.type == .service)
}
.onDelete(perform: paymentViewModel.removeItemFromList)
.listRowBackground(Color.clear)
}
}
.listStyle(.inset)
Spacer()
footer
.padding()
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Payment")
.navigationBarItems(leading: btnBack)
.navigationBarBackButtonHidden(true)
}
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.
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])
}
}
}
}
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
}
}
}
}
}