Skip to content

Instantly share code, notes, and snippets.

@Tprezioso
Created January 24, 2024 12:48
Show Gist options
  • Save Tprezioso/12d40a95c440e2e7be506c1a4fac8da0 to your computer and use it in GitHub Desktop.
Save Tprezioso/12d40a95c440e2e7be506c1a4fac8da0 to your computer and use it in GitHub Desktop.
Tab bar custom badge swift
GeometryReader { geometry in
ZStack(alignment: .bottomLeading) {
// TabView
TabView(selection: $selectedTab) {
Text("First View")
.padding()
.tabItem {
Label("Menu", systemImage: "list.dash")
}.tag(0)
Text("Second View")
.tabItem {
Label("Order", systemImage: "square.and.pencil")
}.tag(1)
Text("Sleep View")
.tabItem {
Label("Phinoz", systemImage: "square.and.pencil")
}.tag(2)
Text("Phinoz View")
.tabItem {
Label("Phinoz", systemImage: "square.and.pencil")
}.tag(3)
}
// Badge View
ZStack {
Circle()
.foregroundColor(.red)
}
.frame(width: 12)
.offset(x: ((2 * CGFloat(self.badgePosition)) - 1) * (geometry.size.width / (1.8 * self.tabsCount)), y: UIDevice.current.orientation.isLandscape ? -20 : -29)
// .opacity(self.badgeNumber == 0 ? 0 : 1)
}
}.ignoresSafeArea(.keyboard)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment