https://github.com/wyattharrell/segmented-selector
A modern UISegmentedControl built with SwiftUI.
https://github.com/wyattharrell/segmented-selector
swift swiftui
Last synced: 3 months ago
JSON representation
A modern UISegmentedControl built with SwiftUI.
- Host: GitHub
- URL: https://github.com/wyattharrell/segmented-selector
- Owner: wyattharrell
- License: mit
- Created: 2024-12-18T02:45:43.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-01-02T15:54:00.000Z (5 months ago)
- Last Synced: 2025-01-14T03:17:13.587Z (5 months ago)
- Topics: swift, swiftui
- Language: Swift
- Homepage:
- Size: 817 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Segmented Selector
[](https://github.com/wyattharrell/segmented-selector/releases)
[](https://raw.githubusercontent.com/wyattharrell/segmented-selector/master/LICENSE)[](https://github.com/wyattharrell/segmented-selector/issues) [](https://swift.org/package-manager/)

A modern UISegmentedControl built with SwiftUI.
## Requirements
- iOS 17.0+
## Installation
### Swift Package Manager
`segmented-selector` supports SPM. In Xcode, click File -> Swift Packages -> Add Package Dependency, enter https://github.com/wyattharrell/segmented-selector.git. Select the version you’d like to use.
You can also manually add the package to your Package.swift file:
```ruby
.package(url: "https://github.com/wyattharrell/segmented-selector.git", from: "2.0.0")
```## Getting Started
### Create an Enum
This will be used as the datasource for the view. It should conform to `String`, `CaseIterable` & `Identifiable`.
```swift
enum ExampleEnum: String, CaseIterable, Identifiable {
var id: Self { self }case option1 = "Option 1"
case option2 = "Option 2"
case option3 = "Option 3"
}```
### Initialize your Configuration (optional) & SegmentedSelector View
```swift
// Create using the default parameters
let configuration = SegmentedSelectorConfiguration()// Or customize font, color, cornerRadius, etc.
let configuration = SegmentedSelectorConfiguration(
shape: <#T##SegmentedSelectorViewState.Shape#>,
selectedSegmentColor: <#T##Color#>,
backgroundColor: <#T##Color#>,
font: <#T##Font#>,
animation: <#T##Animation#>,
padding: <#T##CGFloat#>,
selectedSegment: <#T##T#>
)
)struct SomeView: View {
@State
var selectedSegment: ExampleEnum = .option1var body: some View {
...
// Use the default configuration
SegmentedSelector(selectedSegment: $selectedSegment)// Or optionally provide your own configuration to the view
SegmentedSelector(
configuration: configuration,
selectedSegment: $selectedSegment
)
...
}
}
```## Appearance
Some supported appearance properties via the `SegmentedSelectorConfiguration` are:
| Property | Type | Description |
|---|---|---|
| `backgroundColor` | `Color` | Background color |
| `selectedSegmentColor` | `Color` | Background color of the selected segment |
| `padding` | `CGFloat` | Padding between the selected segment and the background view |
| `shape` | `Shape` | The shape of the control. Either .roundedRectangle or .capsule |
| `font` | `UIFont` | Um.. the font |
| `animation` | `Animation` | Animation of the segment selection. Can be set to `.bouncy`, `.easeIn`, etc. |