티스토리 뷰
iOS SwiftUI
iOS SwiftUI Custom Segmented Control Flexible Width (커스텀 세그먼트 컨트롤, TabView TabMenu, TabBar) 동적 너비 버전
GonsLab 2025. 1. 1. 00:01728x90
반응형

import SwiftUI
/// 커스텀 세그먼트 뷰 (컨텐츠 크기만큼 동적 너비 버전)
struct SegmentedFlexibleView: View {
let segments: [String]
@Binding var currentPage: Int
@Namespace private var name
var body: some View {
ScrollViewReader { proxy in
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 0) {
ForEach(segments.indices, id: \.self) { index in
Button {
currentPage = index
} label: {
VStack {
Text(segments[index])
.font(.footnote)
.fontWeight(.medium)
.foregroundStyle(currentPage == index ? .orange : Color(uiColor: .systemGray))
ZStack {
Capsule()
.fill(.clear)
.frame(height: 4)
if currentPage == index {
Capsule()
.fill(.orange)
.frame(height: 4)
.matchedGeometryEffect(id: "Tab", in: name)
}
}
}
.padding(.horizontal, 8) // 각 항목의 양쪽 여백 추가
}
.id(index)
}
}
}
.onChange(of: currentPage) { _, newValue in
withAnimation {
proxy.scrollTo(newValue, anchor: .center)
}
}
}
.animation(.spring(duration: 0.3), value: currentPage)
}
}
#Preview {
struct Preview: View {
var segments: [Color] = [.red, .green, .blue, .brown, .purple, .pink, .yellow, .orange, .cyan, .teal, .gray, .black]
@State var currentPage = 0
var body: some View {
VStack {
SegmentedFlexibleView(segments: segments.map { "\($0)" },
currentPage: $currentPage)
TabView(selection: $currentPage) {
ForEach(segments.indices, id: \.self) { index in
segments[index]
}
}
.tabViewStyle(.page(indexDisplayMode: .never))
.scrollTargetBehavior(.paging)
}
.ignoresSafeArea(.all, edges: .bottom)
}
}
return Preview()
}
iOS
Swift
Xcode
Objective-C
SwiftUI
UISegmentedControl
Picker
탭뷰
탭뷰
탭메뉴
728x90
반응형
'iOS SwiftUI' 카테고리의 다른 글
| iOS SwiftUI get color from image (이미지에서 평균 색상 추출) (0) | 2025.03.01 |
|---|---|
| iOS SwiftUI View Aligned Carousel Scrollview Paging (다음 뷰 약간 보이는 페이징 스크롤) (0) | 2025.02.01 |
| iOS SwiftUI Custom Segmented Control Fixed Width (커스텀 세그먼트 컨트롤, TabView TabMenu, TabBar) 크기 고정 버전 (0) | 2025.01.01 |
| iOS SwiftUI AudioPlayer 오디오 플레이어 (Music 음악) (2) | 2024.12.01 |
| iOS SwiftUI 팝오버 모달 (Popover Modal Tooltip Dropdown) (0) | 2024.11.01 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 아이오에스
- Xcode
- 인디케이터
- 프로그레스
- presentationcompactadaptation
- localizable
- swiftUI
- SKPayment
- TabBar
- AppStore
- Language
- picker
- 심사
- Reject
- permission
- indicator
- TabView
- custom segment
- SWIFT
- Authorization
- localizing
- 스위프트
- 다국어
- 현지화
- 로컬라이징
- 테이블뷰
- 리젝
- 엑스코드
- ios
- Infinite Carousel
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
글 보관함