Swift SwiftUI

Navigation from and to views

struct ContentView: View {
    @State private var selection: Int? = 0

    var body: some View {
        NavigationView {
            VStack {
                if selection == 0 {
                    FirstView(selection: $selection)
                } else if selection == 1 {
                    SecondView(selection: $selection)
                } else if selection == 2 {
                    ThirdView(selection: $selection)
                }
            }
        }
    }
}

struct FirstView: View {
    @Binding var selection: Int?

    var body: some View {
        Button("Go to Second View") {
            selection = 1
        }
    }
}

struct SecondView: View {
    @Binding var selection: Int?

    var body: some View {
        Button("Go to Third View") {
            selection = 2
        }
    }
}

struct ThirdView: View {
    @Binding var selection: Int?

    var body: some View {
        Button("Go to First View") {
            selection = 0
        }
    }
}



#Preview {
    ContentView()
}

SwiftUI

Playground

import PlaygroundSupport
import SwiftUI


struct ContentView: View {
    var body: some View {
        Text("Hello World")
    }
}
let vc = UIHostingController(rootView: ContentView())

PlaygroundPage.current.liveView = vc

Shapes

Rectangle()
    .fill(Color.red)
    .frame(width: 200, height: 200)
Circle()
    .fill(Color.blue)
    .frame(width: 50, height: 50)

Try HStack en ZStach(!)

Triangles Shapes

struct ASymbol : View {
    let symbolColor: Color

    var body: some View {
        GeometryReader { geometry in
            Path { path in
                let side = min(geometry.size.width, geometry.size.height)
                path.move(to: CGPoint(x: side * 0.5, y: 0))
                path.addLine(to: CGPoint(x: side, y: side))
                path.addLine(to: CGPoint(x: 0, y: side))
                path.closeSubpath()

                }
                .fill(self.symbolColor)
        }
    }
}
struct ContentView : View {
    var body: some View {
        VStack{
        ASymbol(symbolColor: .green)
        ASymbol(symbolColor: .green).rotationEffect(Angle(degrees: 180))
        }
    }
}

Conditional:

struct ContentView: View {
    var body: AnyView {
        if Int.random(in: .min ... .max).isMultiple(of: 2) {
            return AnyView(Text("Even"))
        } else {
            return AnyView(Image(systemName: "star"))
        }
    }
}
Subscribe to RSS - Swift SwiftUI