Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Last active February 12, 2022 20:47
Show Gist options
  • Save CodeSlicing/635c3187edfb1bcd8a691bd9eb6ec10a to your computer and use it in GitHub Desktop.
Save CodeSlicing/635c3187edfb1bcd8a691bd9eb6ec10a to your computer and use it in GitHub Desktop.
Native source code for CodeSlicing episode exploring overlay / background modifiers vs ZStack - demo-01
//
// OverlayVsZStackDemo01Native.swift
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright © 2021 Adam Fordyce. All rights reserved.
//
import SwiftUI
private func Square(_ color: Color, _ sideLength: CGFloat = 100) -> some View {
Rectangle()
.fill(color)
.frame(width: sideLength, height: sideLength)
}
struct OverlayVsZStackDemo01Native: View {
var body: some View {
let offset: CGFloat = 20
let blueSquare = Square(.blue)
let whiteSquare = Square(.white).offset(x: offset, y: offset)
let yellowSquare = Square(.yellow).offset(x: offset, y: -offset)
let greenSquare = Square(.green).offset(x: -offset, y: -offset)
let redSquare = Square(.red).offset(x: -offset, y: offset)
VStack(spacing: 0) {
blueSquare
.overlay(whiteSquare)
.background(yellowSquare)
.background(greenSquare)
.overlay(redSquare)
ZStack {
greenSquare
yellowSquare
blueSquare
whiteSquare
redSquare
}
}
}
}
struct OverlayVsZStackDemo01Native_Previews: PreviewProvider {
struct OverlayVsZStackDemo01Native_Harness: View {
var body: some View {
OverlayVsZStackDemo01Native()
.padding(100)
.foregroundColor(.white)
.background(Color(white: 0.1))
}
}
static var previews: some View {
OverlayVsZStackDemo01Native_Harness()
.previewLayout(.sizeThatFits)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment