A SwiftUI view that displays app settings sections provided by a TZSettingsAdapter
.
TZSettingsView
is available as a Swift Package. To install it, using Swift Package Manager (SPM):
- In Xcode, go to File > Swift Packages > Add Package Dependency.
- Enter the package repository URL:
https://github.com/TrianglZ/TZSettingsView
- Follow the Xcode prompts to complete installation.
- Create a class that conforms to the
TZSettingsAdapter
protocol and implements thegetSettingsSections()
method. The method should return an array ofTZSettingsView.Section
objects that represent the app settings sections.
class MySettingsAdapter: TZSettingsAdapter {
let theme: TZSettingsView.Section.Theme = .init(color: .black,
font: .system(size: 15, weight: .semibold))
func getSettingsSections() -> [TZSettingsView.Section] {
let section1 = TZSettingsView.Section(title: "Title",
theme: theme,
cells: [Cell.myVehicle, Cell.pointsHistory],
cellHeight: 60)
return [section1]
}
}
extension MySettingsAdapter {
enum Cell {
case myVehicle
case pointsHistory
var toSettinsCell: TZSettingsView.TZSettingConfiguration {
let theme: TZSettingsView.TZSettingCellConfiguration.Theme = .init(color: .black,
tint: .gray,
font: .system(size: 15), hasIndicatorView: true)
switch self {
case .myVehicle:
return .init(type: .navigation(view: Text("myVehicle")),
cellConfiguration: .init(image: Image("myVehicles"),
title: "myVehicles",
theme: theme))
case .pointsHistory:
return .init(type: .navigation(view: Text("pointsHistory")),
cellConfiguration: .init(image: Image("pointsHistory"),
title: "pointsHistory",
theme: theme))
}
}
- Instantiate a
TZSettingsView
view with an instance of yourTZSettingsAdapter
class.
let adapter = MySettingsAdapter()
let settingsView = TZSettingsView(adapter: adapter)
- Add the
TZSettingsView
to your SwiftUI view hierarchy.
struct ContentView: View {
var body: some View {
NavigationView {
settingsView
.navigationTitle("Settings")
}
}
}
An enumeration that defines the different types of cells that can be displayed in a TZSettingsSectionView
.
A cell type that navigate to a given view.
view
: The view to be displayed.
A cell type that displays a url.
url
: The URL to be opened when tapped.
A cell type that displays a toggle switch.
isOn
: A binding to a boolean value that determines the on/off state of the toggle.
A cell type that performs an action when tapped.
action
: The action to be performed.
import SwiftUI
import TZSettingsView
struct ContentView: View {
let adapter = MySettingsAdapter()
var body: some View {
NavigationView {
TZSettingsView(adapter: adapter)
.navigationTitle("Settings")
}
}
}
In this example, MySettingsAdapter
conforms to TZSettingsAdapter
and provides app settings sections.
TZSettingsView
uses MySettingsAdapter
to retrieve the app settings sections and display them in a vertical stack using TZSettingsSectionView
.
The TZSettingsView
instance is added to the ContentView
SwiftUI view hierarchy and is embedded in a NavigationView
.
TZSettingsView
is released under the MIT License.