SwiftUI Search Bar Tutorial
source link: https://www.ioscreator.com/tutorials/swiftui-search-bar-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.
in SwiftUI a Search Bar can be displayed inside a List using the searchable modifier. When using the Search Bar the results will be displayed automatically. In this tutorial a list of countries is displayed and can be searched.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 SwiftUISearchBarTutorial 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
A propery array of countries is declared.
The searchstring holds the text entered inside the search bar.
The regular countries will be displayed when the search bar is empty. When the user inserts the search string, the list is searched with that string and the results are displayed
The searchable modifier displays the Search Bar and hold the searchString binding
Go to the Preview and select Live Preview. The countries list is displayed.
Enter The text to be searched inside the Search Bar and the results will be displayed.
The source code of the SwiftUISearchBarTutorial can be downloaded at the ioscreator repository on Github.
Recommend
-
19
Custom navigation bar title view in SwiftUI 05 Jul 2020 ⋅ 1 min read ⋅ SwiftUI
-
196
Always show search bar in a navigation bar in SwiftUI 19 Jul 2021 ⋅ 2 min read ⋅ SwiftUI
-
15
How to create SwiftUI circular progress bar 05 May 2022 ⋅ 7 min read ⋅ SwiftUI...
-
7
How to change status bar color in SwiftUI 30 Jun 2022 ⋅ 5 min read ⋅ SwiftUI...
-
53
How to change a navigation bar color in SwiftUI on iOS 16 Table of Contents In iOS 16, SwiftUI g...
-
4
In SwiftUI, we can add a button to a navigation bar by putting them in toolbar() modifier.
-
3
You can easily support sarunw.com by checking out this sponsor.
-
2
How to change a Tab Bar item color in SwiftUI 24 Oct 2022 ⋅ 3 min read ⋅ SwiftUI
-
13
Create a mac menu bar app in SwiftUI with MenuBarExtra 16 Jun 2022 ⋅ 4 min read ⋅ ma...
-
16
How to change Status Bar text color in SwiftUI 05 Dec 2022 ⋅ 4 min read ⋅ SwiftUI
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK