- Adding a new environment value
[permalink]
private struct ScreamAtMeKey: EnvironmentKey {
static let defaultValue = false
}
// Set via .environent(\.screamAtMe, true)
extension EnvironmentValues {
var screamAtMe: Bool {
get { self[ScreamAtMeKey.self] }
set { self[ScreamAtMeKey.self] = newValue }
}
}
// Set via .screamAtMe(true)
extension View {
func screamAtMe(_ aaaaaah: Bool) -> some View {
environment(.screamAtMe, aaaaaah)
}
}
- Basic Tab View
[permalink]
struct ContentView: View {
var body: some View {
TabView {
SimplePlan()
.badge(23)
.tabItem {
Label("Simple Plan", systemImage: "house")
}
MultiPlan()
.tabItem {
Label("Simple Plan", systemImage: "house.lodge")
}
}
}
}
- Default to a particular tab bar tab
[permalink]
enum Tab: Hashable {
case simplePlan
case complicatedPlan
case multiPlan
var id: Self { self }
}
struct ContentView: View {
@State private var tab = Tab.complicatedPlan
var body: some View {
TabView(selection: $tab) {
SimplePlan(captureModel: SimpleCaptureModel.shared)
.tabItem {
Label("Simple Plan", systemImage: "house")
}
.tag(Tab.simplePlan)
CompliPlan(captureModel: CompliCaptureModel.shared)
.tabItem {
Label("Complicated Plan", systemImage: "house.circle.fill")
}
.tag(Tab.complicatedPlan)
MultiPlan()
.tabItem {
Label("Multi Plan", systemImage: "house.lodge")
}
.tag(Tab.multiPlan)
}
}
}
- Easily giving buttons a border/background
[permalink]
I still dislike the iOS 7 "here's some random blue text. Really, it's a button". I like buttons to look like buttons. You can add a `.buttonStyle` to a view, and it'll apply the style to al the buttons inside.
Here's the different out of the box styles. You can also make your own custom button style.
.buttonStyle(.bordered) // gray filled oval
.buttonStyle(.borderedProminent) // blue filled oval
.buttonStyle(.borderless) // default (I think) - free floating random blue text
.buttonStyle(.plain) // free floating random plain text. that's also a button
- Making a custom button style
[permalink]
struct CustomButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding()
.background(.purple)
.foregroundStyle(.green)
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
.scaleEffect(configuration.isPressed ? 2.0 : 1)
.animation(.easeOut(duration: 0.05), value: configuration.isPressed)
}
}
(please don't actually make buttons with this style...)
And then attach it to something (say an individual button, or a top-level view) like
.buttonStyle(CustomButtonStyle())
- Making a simple button
[permalink]
Button("Button Text") {
print("Snorgle")
}
- Wrapping a UIView
[permalink]
Given a UIView, add it to SwiftUI via UIViewRepresentable.
struct PlaceholderContainerView: UIViewRepresentable {
func makeUIView(context: Context) -> PlaceholderUIView {
return PlaceholderUIView()
}
func updateUIView(_ uIView: PlaceholderUIView, context: Context) {
print("update ui view (context)")
}
}