Skip to content

Latest commit

 

History

History
66 lines (50 loc) · 1.38 KB

swiftui-tips-and-tricks.md

File metadata and controls

66 lines (50 loc) · 1.38 KB

SwiftUI tips and ticks

  • Resume the live preview

    Option-Cmd-P

  • Make @State private

    • @ObjectBinding shared between views
    • @EnvironmentObject shared by many views
    • @State used by the local view
  • Prototype with constant bindings

    TextField(.constant("Hello")).textFieldStyle(.roundedBorder)
    Slider(value: .constant(0.5))
    
    
  • Combine text views

    struct ContentView : View {
     var body: some View {
         Text("Colored ")
             .color(.red)
         +
         Text("SwifUI ")
                 .color(.green)
         +
         Text("Text")
             .color(.blue)
     }
    }
    
  • How to make print() work

    right-click on the play button in the preview canvas and choose Debug Preview. With that small change made you’ll find your print() calls work as normal.

  • Create custom modifiers

    struct PrimaryLabel: ViewModifier {
      func body(content: Content) -> some View {
          content
              .padding()
              .background(Color.black)
              .foregroundColor(Color.white)
              .font(.largeTitle)
              .cornerRadius(10)
      }
     }
      struct ContentView : View {
      var body: some View {
          Text("Hello World")
              .modifier(PrimaryLabel())
      }
     }
    

References

SwiftUI tips and tricks