일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- mvvm
- SWIFT
- self
- viewlifecycle
- escaping
- Refresh
- singleton
- 고차함수
- 글또
- IOS
- ScrollView
- Observer
- apns
- http
- Animation
- NotificationCenter
- struct
- error
- 화면전환
- Git
- segue
- Switch
- list
- SWIFTUI
- calendar
- class
- array
- uikit
- PushNotification
- protocol
- Today
- Total
seong_hye, the developer
오류 해결) Ambiguous use of toolbar 해결 방법 본문
📌 Ambiguous use of toolbar 📌
보통 SwiftUI의 .toolbar 수정자 (오버로드가 여러 개)와
내 코드/ 프레임워크의 이름 충돌 또는 모호한 클로저 형태 때문에 생김
✅ 옳은 toolbar 사용 방법
.toolbar {... } 안에 무엇을 넣는지에 따라 여러 오버로드가 매칭될 수 있음
항상 배치와 래퍼 타입을 명시하는 것이 필요
➡️ 단일 버튼일 경우
NavigationStack {
Text("test")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Save") {...}
}
}
}
➡️ 여러 버튼일 경우
.toolbar {
ToolbarItemGroup(placement: .navigationBarTrailng) {
Button("Add") { ... }
Button("Edit") {...}
}
}
❌ 문제가 되는 경우
📌 동일 위치에 여러 아이템 정의가 뒤죽박죽일 때
같은 placement에 ToolbarItem을 여러 개 흩어 두는 경우 오류 발생 위험 있음
같은 placement 묶음은 ToolbarItemGroup으로 합치는 것이 좋음
// 같은 위치의 item을 나눠두는 것은 좋지 않음
.toolbar {
ToolbarItem(placement: .navigationTrailing) {
Button("Save") {... }
}
ToolbarItem(placement: .navigationTrailing) {
Button("Edit") {... }
}
}
// Group으로 묶어서 두는 것이 좋음
.toolbar {
ToolbarItemGroup(placement: .navigationBarTrailng) {
Button("Save") { ... }
Button("Edit") {...}
}
}
📌 플랫폼 / OS 가용성 차이 (배경 / 색상 관련 API 혼용
iOS 16+에 추가된 .toolbarBackground, .toolbarColorScheme, .toolbarRole 등을
낮은 타깃에서 사용하게 되면 컴파일러가 오버로드를 고르다 모호해지는 경우가 있음
가용성 체크로 분기하는 것이 좋음
if #available (iOS 16.0, *) {
content
.toolbar {
ToolbarItem(placement: .navigationTrailig) { Button("Done") {...} }
}
.toolbarBackground(.visible, for: .navigationBar)
.toolbarColorScheme(.dark, for: .navigationBar)
} else {
content
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {Button("Done") {...}}
}
}
📌 NavigationView vs NavigationStack 혼용으로 인한 배치 문제
iOS 16에서 NavigationStack이 도움되며 일부 배치 동작이 달라짐
에러 자체의 직접 원인은 아니지만 placement를 명시하면 혼동을 줄임
상단 바 -> .navigationBarLeading / .navigationBarTrailing
하단 바 -> .bottomBar
탭바가 있을 땐 사오항에 따라 .principal, .status등은 피하고 명확한 위치 사용
❌ 내가 문제가 되었던 부분
위에서 만난 방안을 시도한 후에도 문제가 발생했음
확인 결과 뷰 자체 코드에서 문제가 발생한 경우도 있었고
수정자들의 겹쳐서 toolbar 코드를 제대로 인식하지 못한 경우도 있었음
✅ 해당 오류가 발생했다고 toolbar만 확인할 것이 아니라
뷰 자체에서 꼬인 부분이 있나 확인하는 것도 중요!!
'IOS > Error' 카테고리의 다른 글
오류 해결) unrecognized selector sent to class 오류 해결 (0) | 2024.01.06 |
---|---|
오류 해결) On branch is up to date with 'origin/브랜치이름'. 오류 해결 (0) | 2024.01.06 |
오류 해결) Cannot use instance member ' ' within property initializer; property initializers run before 'self' is avaliable 해결 (0) | 2023.08.24 |
오류 해결) List Refreshable 오류 (0) | 2022.12.13 |
오류 해결) swift:600: Fatal error: Index out of range (0) | 2022.10.28 |