35

SwiftUI ActionSheet Tutorial

 4 years ago
source link: https://www.tuicool.com/articles/fyAbQre
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.

An action sheet is a specific style of alert that appears in response to a control or action, and presents a set of two or more choices related to the current context. In this tutorial an action sheet is presented containing 3 buttons.SwiftUI requires Xcode 11 and MacOS Catalina, for which the Betas can be downloaded 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 the Single View App template, and then click Next . Enter SwiftUIActionSheetTutorial as the Product Name, select the Use SwiftUI checkbox, and click Next . Choose a location to save the project on your Mac.

z2IBFza.png!web

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

QF3Ibe3.png!web

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

struct ContentView : View {
    // 1
    @State var isSheetShowing: Bool = false
    
    // 2
    var actionSheet: ActionSheet {
        ActionSheet(title: Text("Action Sheet"),
            message: Text("Choose Option"),
            buttons: [
                .default(Text("Delete"), onTrigger: {
                    self.isSheetShowing = false
                }),
                .default(Text("Save"), onTrigger: {
                    self.isSheetShowing = false
                }),
                .destructive(Text("Cancel"), onTrigger: {
                    self.isSheetShowing = false
                })
            ]
        )
    }

    var body: some View {
        // 3
        Button(action: {
            self.isSheetShowing = true
        }) {
            Text("Display Action Sheet")
        }
        .presentation($isSheetShowing) 
    }
}
  1. A state property is declared to determine when the action sheet will be displayed.

  2. The actionsheet property is declared. The actionsheet contains two default and one destructive button. When selected the isSheetShowing property is set to false.

  3. A button is displayed. When clicked the action sheet is presented.

Go to the preview pane. Click the live preview button and click on the “display action sheet” button. The action sheet will be presented.

26BFfmb.png!web

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


Recommend

  • 23

    README.md

  • 62
    • www.tuicool.com 4 years ago
    • Cache

    SwiftUI Getting Started Tutorial

    At WWDC19 Apple introduces SwiftUI, which is a native declarative UI framework. SwiftUI let developers declare what they want to put in the UI and SwiftUI makes sure all rules are enforced. SwiftUI is also cross-platform...

  • 98
    • www.tuicool.com 4 years ago
    • Cache

    SwiftUI Text View Tutorial

    The text view is a view that displays one ore more lines of read-only text. In this tutorial the text views will be modified with different type of modifiers.SwiftUI requires Xcode 11 and MacOS Catalina, for which the Be...

  • 49
    • www.tuicool.com 4 years ago
    • Cache

    SwiftUI State Tutorial

    State is a value, or a set of values, that can change and that affects a view’s behavior, content, or layout. In this tutorial a toggle is created which will determine if a user is logged in or not. .SwiftUI requires Xco...

  • 48
    • www.tuicool.com 4 years ago
    • Cache

    SwiftUI Alert Tutorial

    An Alert displays an alert message to the user. The alert will be displayed using the presentation method which can added to a view. In this tutorial we will display an alert when the user clicks a button.SwiftUI require...

  • 42
    • www.tuicool.com 4 years ago
    • Cache

    SwiftUI Integrate with UIKit Tutorial

    At the current state of SwiftUI not all views are implemented yet. UIKit views can be added in the Swift hierarchy. In this tutorial a url will be displayed in a web view.SwiftUI requires Xcode 11 and MacOS Catalina, for...

  • 28
    • www.tuicool.com 4 years ago
    • Cache

    SwiftUI Segmented Control Tutorial

    A segmented control is a set of two or more segments, each of which functions as a mutually exclusive button. In this tutorial a segmented control will be displayed and a text view will be updated with the contents of th...

  • 28
    • www.tuicool.com 4 years ago
    • Cache

    SwiftUI Picker Tutorial

    A picker includes one or more scrollable lists of values, each of which has a single selected value. In this tutorial a picker is displayed containing colors.SwiftUI requires Xcode 11 and MacOS Catalina, for which the Be...

  • 23

    react-native-actions-sheet A highly customizable cross platform ActionSheet for react native. Screenshots

  • 1

    仿照微博、微信弹出ActionSheet样式 Jan 22nd, 2016 3:08 pm | 评论

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK