Skip to content

Instantly share code, notes, and snippets.

@Matt54
Created October 22, 2023 11:43
Show Gist options
  • Select an option

  • Save Matt54/29cf98f72f8f8589fb818e2dccb20cf9 to your computer and use it in GitHub Desktop.

Select an option

Save Matt54/29cf98f72f8f8589fb818e2dccb20cf9 to your computer and use it in GitHub Desktop.
//
// 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