Skip to content

Instantly share code, notes, and snippets.

@ryanlintott
Last active March 8, 2024 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlintott/06e25c3945224b4dd5e3d821ef5027ec to your computer and use it in GitHub Desktop.
Save ryanlintott/06e25c3945224b4dd5e3d821ef5027ec to your computer and use it in GitHub Desktop.
A SwiftUI example of an alert window that would be easy to implement using AutoLayout. This implementation uses onSizeChange from FrameUp https://github.com/ryanlintott/FrameUp
//
// AutoLayoutTestApp.swift
// AutoLayoutTest
//
// Created by Ryan Lintott on 2024-03-07.
//
import FrameUp
import SwiftUI
@main
struct AutoLayoutTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.windowResizability(.contentSize)
}
}
struct ContentView: View {
let alertText =
"""
Layouts like this are quite simple with AutoLayout and are also possible in SwiftUI.
This is one of the reasons I made FrameUp. I think SwiftUI is production ready. Actually, I think it will make UIs better and more flexible for things beyond specific AutoLayout examples that are a bit tricky to implement.
"""
@State private var maxButtonWidth: CGFloat? = nil
@State private var height: CGFloat? = nil
var body: some View {
VStack(alignment: .leading, spacing: 12) {
HStack(alignment: .top) {
Image(systemName: "exclamationmark.triangle.fill")
.resizable()
.scaledToFit()
.symbolRenderingMode(.multicolor)
.frame(height: 60)
.padding(4)
VStack(alignment: .leading, spacing: 12) {
Text("Simple with FrameUp for SwiftUI")
.font(.headline)
Text(alertText)
}
}
HStack {
Button {
} label: {
Text("Lots of text here.")
.fixedSize()
.onSizeChange { size in
maxButtonWidth = max(maxButtonWidth ?? .zero, size.width)
}
.frame(maxWidth: maxButtonWidth)
}
Spacer()
Button {
} label: {
Text("Cancel")
.fixedSize()
.onSizeChange { size in
maxButtonWidth = max(maxButtonWidth ?? .zero, size.width)
}
.frame(maxWidth: maxButtonWidth)
}
Button {
} label: {
Text("OK")
.fixedSize()
.onSizeChange { size in
maxButtonWidth = max(maxButtonWidth ?? .zero, size.width)
}
.frame(minWidth: .zero, maxWidth: maxButtonWidth)
}
}
}
.padding(24)
.background {
Color.clear
.onSizeChange { size in
height = size.height
}
}
.fixedSize(horizontal: false, vertical: true)
.frame(minWidth: 300)
.frame(height: height)
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment