How to separate the (Add Load) Button from the list? I want to place the button somewhere else and the list to appear at the bottom or centre, as per my need. When I remove the "Add Load" button from the list , the list doesn't show up. And is there any way, I can create a fixed size for a list so that it doesn't take up the whole screen and only appears in a certain size of the screen.
import SwiftUI
struct MainView: View {
@State var br = Double()
@State var loadpay = Double()
@State var gp: Double = 0
@State var count: Int = 1
@State var listcheck = Bool()
@StateObject var taskStore = TaskStore()
func addNewToDo() {
taskStore.tasks.append(Task(id: String(taskStore.tasks.count 1), toDoItem: "load \(count)", amount: Double(gp)))
}
func stepcount() {
count = 1
}
var body: some View {
Form {
HStack(spacing: 15) { Button(String(format: "Add Load"), action: { print("Load is ")
gp = loadpay
})
Button(action: { addNewToDo(); stepcount() }, label: {
Text("")
})
}
ForEach(self.taskStore.tasks) {
task in
Text(task.toDoItem)
}
}
}
}
struct Task: Identifiable {
var id = String()
var toDoItem = String()
var amount: Double = 0
}
class TaskStore: ObservableObject {
@Published var tasks = [Task]()
}
CodePudding user response:
You can wrap your Form
in a VStack
and have the Button
s be sibling elements to the Form
:
struct MainView: View {
@State var br = Double()
@State var loadpay = Double()
@State var gp: Double = 0
@State var count: Int = 1
@State var listcheck = Bool()
@StateObject var taskStore = TaskStore()
func addNewToDo() {
taskStore.tasks.append(Task(id: String(taskStore.tasks.count 1), toDoItem: "load \(count)", amount: Double(gp)))
}
func stepcount() {
count = 1
}
var body: some View {
VStack {
Button(action: { gp = loadpay }) {
Text("Add Load")
}
Button(action: {
addNewToDo()
}) {
Text("Add to do")
}
Form {
ForEach(self.taskStore.tasks) {
task in
Text(task.toDoItem)
}
}
}
}
}