Skip to content

Instantly share code, notes, and snippets.

@AmirDaliri
Last active April 18, 2025 13:09
Show Gist options
  • Save AmirDaliri/6067782bb4fda423726e38eb90086def to your computer and use it in GitHub Desktop.
Save AmirDaliri/6067782bb4fda423726e38eb90086def to your computer and use it in GitHub Desktop.
CustomSegment SwiftUI
//
// SegmentView.swift
// CustomSegment
//
// Created by Amir Daliri on 18.04.2025.
//
import SwiftUI
struct SegmentView: View {
@Namespace private var animation
@State private var selected: Segment = .firstIndex
enum Segment: String, CaseIterable {
case firstIndex = "First Index"
case secondIndex = "Second Index"
}
var body: some View {
HStack(spacing: 0) {
ForEach(Segment.allCases, id: \.self) { segment in
Button(action: {
withAnimation(.spring(response: 0.3, dampingFraction: 0.75)) {
selected = segment
}
}) {
Text(segment.rawValue)
.font(.system(size: 16, weight: .semibold))
.foregroundColor(selected == segment ? .white : .white.opacity(0.8))
.frame(maxWidth: .infinity)
.frame(height: 44)
.background(
ZStack {
if selected == segment {
Capsule()
.fill(LinearGradient(
colors: [Color.pink, Color.orange],
startPoint: .topLeading,
endPoint: .bottomTrailing
))
.matchedGeometryEffect(id: "selection", in: animation)
.shadow(color: Color.orange.opacity(0.4), radius: 8, x: 0, y: 4)
}
}
)
}
.buttonStyle(.plain)
}
}
.padding(4)
.background(.ultraThinMaterial)
.clipShape(Capsule())
.overlay(
Capsule()
.stroke(Color.white.opacity(0.1), lineWidth: 1)
)
.shadow(color: Color.black.opacity(0.25), radius: 10, x: 0, y: 4)
.padding()
}
}
#Preview {
SegmentView()
.preferredColorScheme(.dark)
}
@AmirDaliri
Copy link
Author

CustomSegment.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment