Building Custom Styles on SwiftUI Views

Building Custom Styles on SwiftUI Views

Generic SwiftUI views usually have two components Configuration and Style. Configuration is where data for the view resides and Style is where view get build.

In this tutorial, We are going to understand how can we use Styles to make reusable custom views.

Generic SwiftUI views usually have two components Configuration and Style. Configuration is where data for the view resides and Style is where view get build.

View              Style
Toggle           ToggleStyle
Button           PrimitiveButtonStyle
ProgressView     ProgressViewStyle
TextField        TextFieldStyle
etc

For this tutorial, We are going to implement customs ToggleStyle and PrimitiveButtonStyle.

*Building custom toggle style, *First we are going to look at _ToggleStyle_protocol.

public protocol ToggleStyle {

/// A view that represents the appearance and interaction of a toggle.
    associatedtype Body : View
    func makeBody(configuration: Self.Configuration) -> Self.Body
/// The properties of a toggle instance.
    typealias Configuration = ToggleStyleConfiguration
}

As we can see if we can implement *makeBody *we can implement out own custom ToggleStyle but what is this *Configuration. *Let’s checkout the definition.

public struct ToggleStyleConfiguration {
    /// A type-erased label of a toggle.
    public struct Label : View {
        public typealias Body = Never
    }
    public let label: ToggleStyleConfiguration.Label
    public var isOn: Bool { get nonmutating set }
    public var $isOn: Binding<Bool> { get }
}
///Basic Toggle Implementation:
Toggle(isOn: <state isOn>, label: {
    <Body Label>
}).toggleStyle(<style>)

programming ios swift swiftui mobile-app-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Top Swift iOS App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Best Swift App Development Company

Apple Swift App Development India: Mobile App Development India is an expert iOS Swift App Development company in India. We develop secure and robust mobile app solutions based on new Apple Swift language.

Best Mobile App Development Company | Android and iOS Apps

iPrism Tech is a one of the best and offshore mobile app development company in India, Saudi Arabia and USA. We are a major providers of android, iphone and ipad mobile app development services at economical prices.

iOS Mobile App Development Company in USA

AppClues Infotech is the best mobile app development company in New York that offers custom mobile app development & design services for Android and iOS.