SwiftUI Views & Layouts

Jun 20 2024 · Swift 5.10, iOS 17.4, Xcode 15.3

Lesson 03: Building Views

Demo 2

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

ViewThatFits will choose the view that better fits the current available space. But before you start using it, you’ll refactor code and create custom views that ViewThatFits may choose from.

@Binding var color: Color
@Binding var red: Double
@Binding var green: Double
@Binding var blue: Double
VStack(spacing: 16) {
  ColorCardView(color: color)

  RGBSlidersStackView(
    color: $color,
    red: $red,
    green: $green,
    blue: $blue
  )
}
PortraitColorPicker(
  color: .constant(.red),
  red: .constant(50),
  green: .constant(50),
  blue: .constant(50)
)
.padding()
@Binding var color: Color
@Binding var red: Double
@Binding var green: Double
@Binding var blue: Double
HStack(spacing: 16) {
  ColorCardView(color: color)

  RGBSlidersStackView(
    color: $color,
    red: $red,
    green: $green,
    blue: $blue
  )
}
#Preview(traits: .landscapeLeft) {
  LandscapeColorPicker(
    color: .constant(.red),
    red: .constant(50),
    green: .constant(50),
    blue: .constant(50)
  )
}
@Environment(\.horizontalSizeClass) var horizontalSizeClass
@Environment(\.verticalSizeClass) var verticalSizeClass

var landscapeIsCompact: Bool {
  horizontalSizeClass == .compact && verticalSizeClass == .compact ||
    horizontalSizeClass == .regular && verticalSizeClass == .compact
}

var layout: AnyLayout {
  landscapeIsCompact ?
    AnyLayout(HStackLayout(spacing: 16)) :
    AnyLayout(VStackLayout(spacing: 16))
}
ViewThatFits {
  PortraitColorPicker(
    color: $color,
    red: $red,
    green: $green,
    blue: $blue
  )

  LandscapeColorPicker(
    color: $color,
    red: $red,
    green: $green,
    blue: $blue
  )
}
.padding()
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 2 Next: Conclusion