Skip to content

Instantly share code, notes, and snippets.

@HarshilShah
Created October 31, 2023 10:40
Show Gist options
  • Save HarshilShah/9395924eda532bb1579d7f975d252331 to your computer and use it in GitHub Desktop.
Save HarshilShah/9395924eda532bb1579d7f975d252331 to your computer and use it in GitHub Desktop.
A SwiftUI modifier that allows you to apply only selected edges from some insets as padding
import SwiftUI
public extension EdgeInsets {
init(_ edges: Edge.Set, _ insets: EdgeInsets) {
func inset(for edge: Edge) -> CGFloat {
return edges.contains(Edge.Set(edge)) ? insets[edge] : 0
}
self.init(
top: inset(for: .top),
leading: inset(for: .leading),
bottom: inset(for: .bottom),
trailing: inset(for: .trailing)
)
}
subscript(_ edge: Edge) -> CGFloat {
switch edge {
case .top: return top
case .leading: return leading
case .bottom: return bottom
case .trailing: return trailing
}
}
}
public extension View {
func padding(_ edges: Edge.Set, _ insets: EdgeInsets) -> some View {
self.padding(EdgeInsets(edges, insets))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment