0% found this document useful (0 votes)
3 views5 pages

SwiftUI Notes

SwiftUI is Apple's declarative framework for building user interfaces across its platforms using Swift. It includes components like Views, Modifiers, and State Management, allowing developers to create interactive and dynamic applications. Key features include navigation with NavigationStack, form inputs, and basic networking capabilities for fetching data.

Uploaded by

hy2p8jwfzv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

SwiftUI Notes

SwiftUI is Apple's declarative framework for building user interfaces across its platforms using Swift. It includes components like Views, Modifiers, and State Management, allowing developers to create interactive and dynamic applications. Key features include navigation with NavigationStack, form inputs, and basic networking capabilities for fetching data.

Uploaded by

hy2p8jwfzv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SwiftUI Notes

SwiftUI Basics

What is SwiftUI?

SwiftUI is Apples declarative framework for building UI on iOS, macOS, watchOS, and tvOS using Swift.

Structure:

- View: A UI component (e.g., Text, Button, Image).

- Body: The computed property that returns the view's content.

Example:

struct ContentView: View {

var body: some View {

Text("Hello, SwiftUI!")

Common Views

Text("...") Displays text

Image("name") Shows an image from assets

Button("Label") { action } Clickable button

VStack, HStack, ZStack Stack views vertically, horizontally, or overlapping

Spacer() Adds flexible space

List Scrollable list

ScrollView Scrollable container


SwiftUI Notes

Modifiers

Modifiers change a views appearance or behavior.

Example:

Text("Hello")

.font(.title)

.foregroundColor(.blue)

.padding()

Common Modifiers:

- .font(.title): Font style

- .foregroundColor(.red): Text color

- .padding(): Adds spacing

- .background(Color.gray): Background color

- .cornerRadius(10): Rounded corners

- .frame(width: 100, height: 100): Sets size

State Management

@State For local view state

@Binding Share state between views

@ObservedObject & @StateObject Used with external, observable models

Example:

@State private var count = 0


SwiftUI Notes

Button("Tap") {

count += 1

Navigation & Layout

NavigationStack (iOS 16+):

NavigationStack {

NavigationLink("Go", destination: DetailView())

TabView:

TabView {

Text("Home")

.tabItem {

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

Text("Settings")

.tabItem {

Label("Settings", systemImage: "gear")

}
SwiftUI Notes

Forms & Inputs

Form {

TextField("Enter name", text: $name)

Toggle("Enable feature", isOn: $isEnabled)

Stepper("Count: \(count)", value: $count)

Networking (Basic Example)

struct User: Codable, Identifiable {

let id: Int

let name: String

@State private var users = [User]()

func fetchUsers() {

guard let url = URL(string: "https://example.com/users") else { return }

URLSession.shared.dataTask(with: url) { data, _, _ in

if let data = data {

if let decoded = try? JSONDecoder().decode([User].self, from: data) {

DispatchQueue.main.async {

self.users = decoded

}
SwiftUI Notes

}.resume()

Previews

struct ContentView_Previews: PreviewProvider {

static var previews: some View {

ContentView()

You might also like