Created
October 22, 2023 11:43
-
-
Save Matt54/29cf98f72f8f8589fb818e2dccb20cf9 to your computer and use it in GitHub Desktop.
Simple custom SwiftUI tabbar based on https://www.youtube.com/watch?v=6W2vuwWTAU4&list=PLimqJDzPI-H9h7HS40v-5QdwxoC4jrXQu&index=1
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
| // | |
| // CustomTabBarExample.swift | |
| // | |
| // Created by Matt Pfeiffer on 10/22/23. | |
| // | |
| import SwiftUI | |
| struct CustomTabBarExample: View { | |
| @State var index: Int = 0 | |
| var body: some View { | |
| ZStack { | |
| Color.red.hueRotation(Angle(degrees: Double(90 * index+1))) | |
| CustomTabBar(index: $index) | |
| } | |
| } | |
| } | |
| #Preview { | |
| CustomTabBarExample() | |
| } | |
| struct CustomTabBar: View { | |
| @Binding var index: Int | |
| var body: some View { | |
| VStack { | |
| Spacer() | |
| CustomTabs(index: $index) | |
| } | |
| } | |
| } | |
| struct CustomTabs: View { | |
| @Binding var index: Int | |
| let images: [Image] = [Image(systemName: "pencil.circle"), | |
| Image(systemName: "eraser"), | |
| Image(systemName: "scribble"), | |
| Image(systemName: "pencil.tip")] | |
| var body: some View { | |
| HStack { | |
| ForEach(0...3, id: \.self) { index in | |
| Button(action: { | |
| self.index = index | |
| }, label: { | |
| images[index] | |
| .resizable() | |
| .frame(width: 24, height: 24) | |
| }) | |
| .foregroundColor(Color.black.opacity(self.index == index ? 1 : 0.2)) | |
| .frame(maxWidth: .infinity) | |
| // middle button | |
| if index == 1 { | |
| Button(action: { | |
| }, label: { | |
| Image(systemName: "plus.circle.fill").renderingMode(.original) | |
| .resizable() | |
| .frame(width: 48, height: 48) | |
| }) | |
| .offset(y: -25) | |
| .frame(maxWidth: .infinity) | |
| } | |
| } | |
| } | |
| .padding(.top, 35) | |
| .background(Color.white) | |
| .clipShape(CShape()) | |
| } | |
| } | |
| struct CShape: Shape { | |
| func path(in rect: CGRect) -> Path { | |
| return Path { path in | |
| path.move(to: CGPoint(x: 0, y: 35)) | |
| path.addLine(to: CGPoint(x: 0, y: rect.height)) | |
| path.addLine(to: CGPoint(x: rect.width, y: rect.height)) | |
| path.addLine(to: CGPoint(x: rect.width, y: 35)) | |
| path.addArc(center: CGPoint(x: rect.width / 2, y: 35), | |
| radius: 35, | |
| startAngle: .zero, | |
| endAngle: .init(degrees: 180), | |
| clockwise: true) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment