SwiftUI Button as EditButton
The implementation below replaces EditButton
's functionality with a Button
:
import SwiftUI
struct ContentView: View {
@State var isEditing = false
@State var selection = Set<String>()
var names = ["Karl", "Hans", "Faustao"]
var body: some View {
NavigationView {
VStack {
List(names, id: \.self, selection: $selection) { name in
Text(name)
}
.navigationBarTitle("Names")
.environment(\.editMode, .constant(self.isEditing ? EditMode.active : EditMode.inactive)).animation(Animation.spring())
Button(action: {
self.isEditing.toggle()
}) {
Text(isEditing ? "Done" : "Edit")
.frame(width: 80, height: 40)
}
.background(Color.yellow)
}
.padding(.bottom)
}
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
Result
However, by doing so, selection handling needs to be implemented by our own (which may or may not be an issue).
Unfortunately there isn't much documentation around that at this point:
https://developer.apple.com/documentation/swiftui/list/3367016-init
How do I get an edit button for a row in a list/for each loop? [SwiftUI]
You need to display the button yourself.
First, ensure you have some @State
var tracking whether Edit mode is toggled:
@State var editing: Bool = false
Then, in your body
, show the circle button next to the NavigationLink
if you are editing
:
var body: some View {
ZStack {
List {
ForEach(searchResults, id: \.self.id) { dir in
Section(dir.title) {
ForEach(dir.getChildFolders(), id: \.self.id) { folder in
HStack {
NavigationLink(destination: DirectoryView(directory: folder)) {
Label(folder.title, systemImage: "folder")
}
if editing {
Spacer()
Button(action: {
// Perform circle button action here
}) {
Image(systemName: "ellipsis.circle")
}
}
}
}
.onDelete(perform: { offsets in
dir.items.remove(atOffsets: offsets)
updateView.update()
})
.onMove(perform: { source, destination in
dir.items.move(fromOffsets: source, toOffset: destination)
updateView.update()
})
}
}
}
}
}
SwiftUI change EditButton Text Color
It is not possible to use accentColor if you Edit button in List, you have to set a navigation trail button and need to use environment editmode
@State private var editMode: EditMode = .inactive
var body: some View {
List {
ForEach(viewModel.datas) { data in
Text(data)
}
.onDelete { offset in
self.indexSetToDelete = offset
}
}.environment(\.editMode, $editMode)
.navigationBarItems(trailing: editButton) }
private var editButton: some View {
return Button {
if editMode == .inactive {
editMode = .active
} else {
editMode = .inactive
}
} label: {
Text(editMode == .inactive ? "Edit" : "Done")
.body(color: Color.red)
}
}
SwiftUI EditButton action on Done
From what I understand, the EditButton is meant to put the entire environment in edit mode. That means going into a mode where, for example, you rearrange or delete items in a list. Or where text fields become editable. Those sorts of things. "Done" means "I'm done being in edit mode", not "I want to apply my changes." You would want a different "Apply changes," "Save," "Confirm" or whatever button to do those things.
Swift Change EditButton()
Found it out:
Works as seen here:
SwiftUI change EditButton Text Color
In my Case:
var editButton: some View {
return Button {
if editMode == .inactive {
editMode = .active
} else {
editMode = .inactive
}
} label: {
Image(systemName: "plus.circle")
.padding(.top)
.font(.title)
.foregroundColor(Color.red)
}
}
SwiftUI EditButton in HStack not activating edit mode
Here is working solution - looks like they require that EditButton was a root view of section, so we can construct everything else above it. (tested with Xcode 11.4 / iOS 13.4)
Note: @Environment(\.editMode) var editMode
is not needed
Section(header:
EditButton().frame(maxWidth: .infinity, alignment: .trailing)
.overlay(Text("Header"), alignment: .leading)
)
{
ForEach(items, id: \.self) { item in
Text(item)
}
.onMove(perform: reorderItems)
.onDelete(perform: deleteItems)
}
SwiftUI - Dynamic Edit Button on Nav Bar
If you are ok with just supporting iOS 14+ you should use the toolbar api
.toolbar {
ToolbarItem(placement: .primary) {
if self.addingItems {
Button("Done", action: submitItems)
} else {
EditButton()
}
}
}
Related Topics
Swiftui Navigation on iPad - How to Show Master List
Strange Error Nw_Protocol_Get_Quic_Image_Block_Invoke Dlopen Libquic Failed
Round Top Corners of a Uiview in Swift
Cannot Use Mutating Member on Immutable Value of Type
Getting Path for Resource in Command Line Tool
Realm Mobile Platform, How to Connect While Offline
How to Create a Static Class in Swift
Nsnumberformatter:Show 'K' Instead of ',000' in Large Numbers
Why Does Swift Return an Unexpected Pointer When Converting an Optional String into an Unsafepointer
Render a 3D Model (Hair) with Semi-Transparent Texture in Scenekit
How to Convert a Swift String to Cfstring
Swift "Where" Array Extensions
Using Swift to Disable Sleep/Screen Saver for Osx
How to View Value of Swift "Let" Constant in Xcode 6 Debugger
How to Get Next Case of Enum(I.E. Write a Circulating Method) in Swift 4.2