2

SwiftUI Custom Swipe Actions Tutorial

 2 years ago
source link: https://www.ioscreator.com/tutorials/swiftui-custom-swipe-actions-tutorial
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
SwiftUI Custom Swipe Actions Tutorial

in SwiftUI a custom swipe action can be displayed when an row on a List is swiped. The action can be displayed left or right and can be displayed with a custom tint color and a system image.. In this tutorial some swipe actions can be triggered which will update the navigation title. This tutorial is built for iOS15 and Xcode 13, which can be download at the Apple developer portal.

Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project. In the template selector, select iOS as the platform, select App template in the Application section and then click Next.

Enter SwiftUICustomSwipeActionsTutorial as the Product Name, select SwiftUI as Interface, SwiftUI App as Life Cycle and Swift as Language. Deselect the Include Tests checkbox and click Next. Choose a location to save the project on your Mac.

In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.

In the Project navigator, click to select ContentView.swift. Change the ContentView struct to

struct ContentView: View { // 1. @State private var action = "" var body: some View { NavigationView { List { ForEach(1..<6) { i in Text("Item \(i)") // 2. .swipeActions(edge: .leading, allowsFullSwipe: false) { Button { action = "Star" } label: { Label("Star", systemImage: "star.circle") } .tint(.yellow) } // 3. .swipeActions(edge: .trailing) { Button(role: .destructive){ action = "Trash" } label: { Label("Trash", systemImage: "trash.circle") } } // 4. .swipeActions(edge: .trailing) { Button{ action = "Flag" } label: { Label("Flag", systemImage: "flag.circle") } } } } // 5. .navigationTitle("\(action)") } } }
  1. A propery array of action is declared and will be used for the navigation title

  2. The swipeActions modifier contains a button with the star systemImage. The allowfullswipe parameter is used to auto select the action when a full swipe is inititated. Here this is disabled.

  3. The swipeActions modifier contains a button with a trash systemImage. The destructive role display the action in a red color.

  4. A blue button item is displayed containing the flag systemimage.

  5. The navigationTitle is updated when the action is triggered.

Go to the Preview and select Live Preview. Swipe on a item from the List to initiate a custom action. The navigation Title will be updated with the corresponding Label title.

The source code of the SwiftUICustomSwipeActionsTutorial can be downloaded at the ioscreator repository on Github.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK