Created
February 15, 2021 10:06
-
-
Save prafullakumar/e7f85e58b80f6c4af1bc53cd8673e087 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
struct ContentView: View { | |
@State private var badgeNumber: Int = 10 | |
private var badgePosition: Int = 1 | |
private var tabsCount: CGFloat = 2 | |
@State private var selectedTab = 0 | |
@State private var name = "" | |
var body: some View { | |
GeometryReader { geometry in | |
ZStack(alignment: .bottomLeading) { | |
// TabView | |
TabView(selection: $selectedTab) { | |
TextField("Enter your name", text: $name) | |
.padding() | |
.tabItem { | |
Label("Menu", systemImage: "list.dash") | |
}.tag(0) | |
Text("Second View") | |
.tabItem { | |
Label("Order", systemImage: "square.and.pencil") | |
}.tag(1) | |
} | |
// Badge View | |
ZStack { | |
Circle() | |
.foregroundColor(.red) | |
Text("\(self.badgeNumber)") | |
.foregroundColor(.white) | |
.font(Font.system(size: 12)) | |
} | |
.frame(width: 20, height: 20) | |
.offset(x: ( ( 2 * CGFloat(self.badgePosition)) - 1 ) * ( geometry.size.width / ( 2 * self.tabsCount ) ), y: -30) | |
.opacity(self.badgeNumber == 0 ? 0 : 1) | |
} | |
}.ignoresSafeArea(.keyboard) //otherwise badge view will float | |
} | |
} | |
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