0Pricing
SwiftUI Academy · Lekcja

ButtonStyle i LabelStyle

Twórz markowe style elementów sterujących wielokrotnego użytku

ButtonStyle i LabelStyle to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej SwiftUI Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Style Controls, Not Instances

Restyling every button by hand gets tedious. A ButtonStyle defines one branded look you reuse across the whole app. 🎨

The ButtonStyle Protocol

Conform a struct to ButtonStyle and implement makeBody. It receives a configuration and returns the styled button.

struct PrimaryStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
    }
}

The label Inside Configuration

The configuration gives you the buttons label, which is the title or content. You decorate that label with padding and color.

configuration.label
    .padding()
    .background(.blue)

React to isPressed

Configuration also exposes isPressed. Use it to dim or scale the button while a finger is down for tactile feedback.

configuration.label
    .opacity(configuration.isPressed ? 0.6 : 1)

Apply with buttonStyle

Attach your style with the .buttonStyle modifier. Every button it touches adopts the same branded appearance.

Button("Save") { }
    .buttonStyle(PrimaryStyle())

Style a Whole Container

Set .buttonStyle on a stack and every button inside inherits it. One line styles a full group of controls.

Built-in Styles Exist Too

SwiftUI ships ready styles like .bordered and .borderedProminent. Reach for those before writing a custom one.

Button("Go") { }
    .buttonStyle(.borderedProminent)

Now Meet LabelStyle

A Label pairs text with an icon. A LabelStyle controls how that pair is arranged and shown.

Label("Home", systemImage: "house")

Title-Only or Icon-Only

Built-in label styles let you show just the title, just the icon, or both. Switch based on space without changing the call site.

Label("Home", systemImage: "house")
    .labelStyle(.iconOnly)

Custom LabelStyle

Conform to LabelStyle and implement makeBody to rearrange the icon and title yourself, like stacking them vertically.

func makeBody(configuration: Configuration) -> some View {
    VStack { configuration.icon; configuration.title }
}

One Definition, Many Buttons

Custom styles keep look and behavior in one place. Tweak the style and every button or label across the app updates together.

Quick Check

Quick check on control styles.

Recap: Reusable Control Looks

Use ButtonStyle and LabelStyle to define branded controls once. React to isPressed, rearrange labels, and apply the look everywhere. 🧠

Często zadawane pytania

Czy lekcja „ButtonStyle i LabelStyle” jest bezpłatna?

Tak — pełny tekst „ButtonStyle i LabelStyle” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu SwiftUI Academy, przejdź na CoddyKit PRO. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „ButtonStyle i LabelStyle”?

Twórz markowe style elementów sterujących wielokrotnego użytku Ćwiczysz SwiftUI Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć SwiftUI Academy?

Nie wymagamy żadnego doświadczenia. SwiftUI Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „ButtonStyle i LabelStyle”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji SwiftUI Academy?

Tak. Każda lekcja SwiftUI Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Wyodrębnianie widoków wielokrotnego użytku
  2. Niestandardowe ViewModifiers
  3. ButtonStyle i LabelStyle
  4. ViewBuilder i generyczne kontenery
← Powrót do SwiftUI Academy