Skip to content

Instantly share code, notes, and snippets.

@ryanashcraft
Last active April 18, 2024 21:01
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 ryanashcraft/87bc6fd517b64d90e2a9ee6f978b163e to your computer and use it in GitHub Desktop.
Save ryanashcraft/87bc6fd517b64d90e2a9ee6f978b163e to your computer and use it in GitHub Desktop.
iOS 16-compatible SwiftUI equivalent of cellLayoutMarginsFollowReadableWidth
import SwiftUI
private struct SafeAreaInsetsFollowReadableWidthModifier: ViewModifier {
static let readableWidth: Double = 672
@ViewBuilder
func body(content: Content) -> some View {
GeometryReader { geometry in
let insetLength = max(0, geometry.size.width - Self.readableWidth) / 2
content
.safeAreaInset(edge: .leading, spacing: 0) {
Color.clear.frame(width: insetLength)
}
.safeAreaInset(edge: .trailing, spacing: 0) {
Color.clear.frame(width: insetLength)
}
}
}
}
public extension View {
func safeAreaInsetsFollowReadableWidth() -> some View {
self.modifier(SafeAreaInsetsFollowReadableWidthModifier())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment