Created
October 31, 2023 10:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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