Where who wants to meet someone

[SwiftUI] tabItem 색상 변경 본문

Apple Developer/iOS(SwiftUI)

[SwiftUI] tabItem 색상 변경

Lust3r 2024. 1. 27. 17:31
728x90
    var body: some View {
        TabView(content: {
            TestView()
                .tabItem {
                    Label("탭 1", systemImage: "house")
                }
        })
    }
  • 위와 같이 TabView 안에 View를 넣고 tabItem modifier를 적용하면 다음과 같이 화면에 보인다

  • 그러나 이 앱에서 내가 원하는 색이 있는데, 어떻게 바꿀 수 있을까 하고 시도한 결과
    background, foreground 다 적용이 안되더라..
  • 검색 결과 accentColor를 사용했던 것 같은데

 

accentColor(_:) | Apple Developer Documentation

Sets the accent color for this view and the views it contains.

developer.apple.com

  • 17.4까지만 지원되는 deprecate될 메서드인 것을 확인..
    대신 asset의 accentColor나 View.tint를 사용하라고 한다.
  • 앱의 Primary Color가 하나라면 AccentColor를 바로 사용했겠지만, 강조색을 변경할 수 있는 기능을 넣고 싶어 몇몇 ColorSet을 추가했기에 tint를 써보기로 했다.
 

tint(_:) | Apple Developer Documentation

Sets the tint color within this view.

developer.apple.com

  • TabView에 tint modifier를 사용하고, 지정한 색상을 사용하면 원하는 색으로 변하는 것을 확인할 수 있다.
    var body: some View {
        TabView(content: {
            TestView()
                .tabItem {
                    Label("탭 1", systemImage: "house")
                }
        })
        .tint(Color("AccentColor_Pink"))
    }