Skip to content

Instantly share code, notes, and snippets.

@StewartLynch
Created February 8, 2025 19:03
Show Gist options
  • Save StewartLynch/fbbd346f8555068e0634a9c5c114af78 to your computer and use it in GitHub Desktop.
Save StewartLynch/fbbd346f8555068e0634a9c5c114af78 to your computer and use it in GitHub Desktop.
OverTopView Template
import SwiftUI
struct <#ViewName#>: View, OverTopable {
let title: String
let choices: [<#Type#>]
let current: <#Type#>
@State var updatedContent: <#Type#>
let hasTwoButtons: Bool
@Binding var showOverTop: Bool
var update: (<#Type#>) -> ()
init(
title: String,
choices: [<#Type#>] = [],
current: <#Type#>,
hasTwoButtons: Bool = false,
showOverTop: Binding<Bool>,
update: @escaping (<#Type#>) -> Void
) {
self.title = title
self.choices = choices
self.current = current
self._updatedContent = State(initialValue: current)
self.hasTwoButtons = hasTwoButtons
self._showOverTop = showOverTop
self.update = update
}
var body: some View {
ZStack {
Color(.systemBackground).opacity(showOverTop ? 0.4 : 1)
.ignoresSafeArea()
.onTapGesture {
withAnimation {
showOverTop = false
}
}
VStack {
Text(title)
.font(.title2)
// Edit content View
HStack {
if hasTwoButtons {
Button("Cancel") {
withAnimation {
showOverTop = false
}
}
.buttonStyle(.bordered)
}
Button("Save") {
withAnimation {
showOverTop = false
update(updatedContent)
}
}
.buttonStyle(.borderedProminent)
}
}
.padding()
.frame(width: 300)
.background(
RoundedRectangle(cornerRadius: 20)
.fill(Color(.systemBackground))
.shadow(color: .black.opacity(0.3), radius: 10, x: 0, y: 5)
)
.offset(y: -100)
}
}
}
#Preview {
<#ViewName#>(
title: "<#Title#>",
choices: [<#Optional array of content - leave empty if not using a selection from array#>],
current: <#Instance of type#>
hasTwoButtons: <#true or false#>,
showOverTop: .constant(false),
update: { update in
// This is the action that will come from the presenting view
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment