SwiftUI Pull to Refresh Tutorial
source link: https://www.ioscreator.com/tutorials/swiftui-pull-to-refresh-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.
The Pull to Refresh functionality can be used to refresh contents of a List by pulling down. SwiftUI introduced the refreshablel() modifier. In this tutorial a JSON file will be fetched from the internet and displayed in a list when the pull to refresh is initiated..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 SwiftUIPullToRefreshTutorial 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. Above the ContentView struct add a new Contact struct to hold the items of the json file which will be loaded.
Change the ContentView struct to
A state propery array of contacts is declared which will hold the contacts. The first item is a stub item which will display the pull to refresh text in the List.
The List is displayed and contains the name and emall of the contact
The refreshable modifier is added to the List and will display an activity indicator while running the code.
The json file is fetched asynchronously and decoded into the Contacts struct.
Go to the Preview and select Live Preview.
Pull down the Pull to refresh item to load and display the contacts.
The source code of the SwiftUIPullToRefreshTutorial can be downloaded at the ioscreator repository on Github.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK