SwiftUI Views & Layouts

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

Lesson 02: Configuring Layouts

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

Now that you know about Device size classes, you can start improving the UI by changing the layout to better use the available space in landscape orientation.

@Environment(\.horizontalSizeClass) var horizontalSizeClass
@Environment(\.verticalSizeClass) var verticalSizeClass
var landscapeIsCompact: Bool {
  horizontalSizeClass == .compact && verticalSizeClass == .compact ||
    horizontalSizeClass == .regular && verticalSizeClass == .compact
}
@Binding var color: Color
@Binding var red: Double
@Binding var green: Double
@Binding var blue: Double
VStack {
  RGBSliderView(rgbSlider: .red, value: $red)
  RGBSliderView(rgbSlider: .green, value: $green)
  RGBSliderView(rgbSlider: .blue, value: $blue)

  Button("Set Color") {
    color = Color(
      red: red / 255,
      green: green / 255,
      blue: blue / 255
    )
  }
  .primaryBackground()
}
RGBSlidersStackView(
  color: .constant(.red),
  red: .constant(50),
  green: .constant(50),
  blue: .constant(50)
)
.padding()
Group {
  if landscapeIsCompact {
    HStack(spacing: 16) {
      ColorCardView(color: color)

      RGBSlidersStackView(
        color: $color,
        red: $red,
        green: $green,
        blue: $blue
      )
    }
  } else {
    VStack(spacing: 16) {
      ColorCardView(color: color)

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