Skip to content

SwiftUI Tab 영역 처리

kangho lee edited this page Aug 27, 2025 · 1 revision

SwiftUI에서 Color.clear나 Spacer()는 콘텐츠 영역이 아니기 때문에 gesture가 먹히지 않음

Case: Coin Cell 눌렀을 때, 인식을 못함

원인: Spacer()를 인식 못함

해결: 전체 영역을 contentsShape(.rect)로 처리

Case: 서치바 키보드 dismiss 처리

원인: 기본 default color가 clear여서 tapGesture 인식을 못함

라이트/다크 모드 대응이 되는 배경 색상을 만들어서 background로 만든 후, tapGesture 담

편의를 위해 Util 생성

import SwiftUI

struct DismissKeyboardOnTap: ViewModifier {
    init() {}
    
    func body(content: Content) -> some View {
        content
            .gesture(
                TapGesture().onEnded {
                    UIApplication.shared.endEditing()
                }
            )
    }
}

extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

extension View {
    func dissmissKeyboardOnTap() -> some View {
        modifier(DismissKeyboardOnTap())
    }
}

Clone this wiki locally