Last active
March 19, 2020 07:25
-
-
Save ivangodfather/acb6cda94b4718e504d924c3e2d62fef to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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