Skip to content

Instantly share code, notes, and snippets.

@zats
Created March 24, 2024 13:21
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 zats/e4e5875d25761eaf2aab3018437ce29b to your computer and use it in GitHub Desktop.
Save zats/e4e5875d25761eaf2aab3018437ce29b to your computer and use it in GitHub Desktop.
SwiftUI inverted List
import UIKit
import SwiftUI
struct RootView: View {
var body: some View {
List {
FlippedCell(text: "Hello")
FlippedCell(text: "Is it me")
FlippedCell(text: "you looking")
FlippedCell(text: "for")
}
.scaleEffect(y: -1)
.listStyle(.plain)
}
}
struct FlippedCell: View {
let text: String
var body: some View {
FlippedCellContext(text: text)
.scaleEffect(y: -1)
.swipeActions(edge: .leading) {
Button(action: {}, label: {
Image(flippedSystemName: "star")
})
.tint(.yellow)
}
.swipeActions(edge: .trailing) {
Button(role: .destructive, action: {}, label: {
Image(flippedSystemName: "trash")
})
}
.contextMenu(menuItems: {
Button("Engage") {}
Button("Hyperdrive") {}
}, preview: {
FlippedCellContext(text: text)
.padding()
})
}
}
struct FlippedCellContext: View {
let text: String
var body: some View {
HStack {
Text(text)
Spacer()
Menu(content: {
Button("Copy", systemImage: "doc.on.doc") {}
Button("Regenerate", systemImage: "bird") {}
}, label: {
Image(systemName: "ellipsis")
.foregroundColor(.secondary)
.padding(.balanced())
.background()
})
}
}
}
extension Image {
init(flippedSystemName systemName: String) {
self.init(uiImage: UIImage(systemName: systemName)?.flipped() ?? UIImage())
}
}
extension UIImage {
func flipped() -> UIImage {
UIGraphicsImageRenderer(size: size).image { context in
context.cgContext.translateBy(x: 0, y: size.height * 0.5)
context.cgContext.scaleBy(x: 1, y: -1)
context.cgContext.translateBy(x: 0, y: -size.height * 0.5)
draw(at: .zero)
}
.withRenderingMode(.alwaysTemplate)
}
}
extension EdgeInsets {
public static func balanced(horizontal: CGFloat = 8, vertical: CGFloat = 8) -> EdgeInsets {
EdgeInsets(top: vertical, leading: horizontal, bottom: vertical, trailing: horizontal)
}
}
import PlaygroundSupport
let viewController = UIHostingController(rootView: RootView())
viewController.view.frame = CGRect(x: 0, y: 0, width: 300, height: 600)
viewController.view.tintColor = .black
PlaygroundPage.current.liveView = viewController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment