

SwiftUI Segmented Control Tutorial
source link: https://www.tuicool.com/articles/rmE3Q3y
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.

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 the selected segment .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 SwiftUISegmentedControlTutorial as the Product Name, select the Use SwiftUI 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 code inside the ContentView struct to
struct ContentView : View { // 1. @State private var selectorIndex = 0 @State private var numbers = ["One","Two","Three"] var body: some View { VStack { // 2 SegmentedControl(selection: $selectorIndex) { ForEach(0 ..< numbers.count) { index in Text(self.numbers[index]).tag(index) } } // 3. Text("Selected value is: \(numbers[selectorIndex])").padding() } } }
-
each item in a selector has a tag value with index number starting at 0.
-
A segmented control is displayed containing the items from the numbers array.
-
The selected value in the segmented control is displayed
In the preview pane select the live preview button. Select a segmented control item and the text view will update.

The source code of the SwiftUISegmentedControlTutorial can be downloaded at the ioscreator repository on Github .
Recommend
-
46
A segmented control is a set of two or more segments, each of which functions as a mutually exclusive button. Within the control, all segments are equal in width. In this tutorial we create 2 segments inside a UISegmented...
-
25
This is just a note on getting the best performance out of an async program. The point of using async IO over blocking IO is that it gives the user program more control over handling IO, on the premise that the use...
-
33
来源: ACL2020 链接: https://arxiv.org/pdf/2005.00856.pdf
-
24
How to create segmented control in SwiftUI Table of ContentsA segmented control is a horizontal control that consists of multiple segments, each segment functioning as a mutually exclusive button
-
8
Comparing coefficients across segmented regression models given aggregated, heterogenous data Cross Validated is a question and answer site for people interested in statistics, machine learning, data a...
-
12
We are glad to roll out the WinUI Segmented Control in our
-
4
Building a Segmented Progress Bar in AndroidLeverage the power of custom drawing.Photo by Daniel Cheung...
-
6
Segmented React Native Segmented control Dec 18, 2021 1 min read segmented-control-rn React Native Segmented control Insta...
-
8
Dynamic Programming Algorithm for Segmented Least Squares Asked 11 years, 1 month ago ...
-
4
Segmented A fully customizable iOS like segmented control component for RN CLI projects Aug 06, 2023 1 min read
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK