Skip to content

Instantly share code, notes, and snippets.

@ivangodfather
Last active March 19, 2020 07:25
Show Gist options
  • Save ivangodfather/acb6cda94b4718e504d924c3e2d62fef to your computer and use it in GitHub Desktop.
Save ivangodfather/acb6cda94b4718e504d924c3e2d62fef to your computer and use it in GitHub Desktop.
import SwiftUI
extension View {
func badge(num: Int) -> some View {
return self.overlay(
ZStack(alignment: .topTrailing) {
Circle()
.fill(Color.red)
.frame(width: 24, height: 24)
.overlay(Text(num.description))
.transition(.scale)
.alignmentGuide(.trailing) { dim -> CGFloat in
return dim[VerticalAlignment.center]
}
.alignmentGuide(VerticalAlignment.top) { dim -> CGFloat in
return dim[HorizontalAlignment.center]
}
}
, alignment: .topTrailing)
}
}
struct ContentView : View {
@State var badgeCount = 0
var body: some View {
VStack {
Rectangle()
.fill(Color.blue)
.frame(width: 100, height: 100)
.badge(num: badgeCount)
.border(Color.green)
Stepper("Update", value: $badgeCount.animation())
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment