SwiftUI로 앱을 개발 중입니다.
NavigationView가 있고 탐색 모음에 버튼이 있습니다. 현재보기 (TabView 선택의 결과)를 다른보기로 바꾸고 싶습니다.
기본적으로 사용자가 "편집"버튼을 클릭하면 편집을 위해보기를 다른보기로 바꾸고 싶습니다. 사용자가 완료되면 "완료"단추를 클릭하면 이전보기로 복원됩니다.
변수를 사용하여 현재 탭보기에 표시되는보기를 동적으로 선택할 수 있지만 이것이 SwiftUI에서 "올바른 방법"이 아닌 것 같습니다. 이렇게하면 전환 시각 효과를 적용 할 수 없습니다.
내가 찾고있는 것을 설명하는 몇 가지 코드 샘플.
private extension ContentView {
@ViewBuilder
var navigationBarLeadingItems: some View {
if tabSelection == 3 {
Button(action: {
print("Edit pressed")
// Here I want to replace the tabSelection 3 view by another view temporarly and update the navigation bar items
}) {
Text("Edit")
}
}
}
}
struct ContentView: View {
var body: some View {
NavigationView {
TabView(selection: $tabSelection) {
ContactPage()
.tabItem {
Text("1")
}
.tag(1)
Text("Chats")
.tabItem() {
Text("2")
}
.tag(2)
SettingsView()
.tabItem {
Text("3")
}
.tag(3)
}.navigationBarItems(leading: navigationBarLeadingItems)
}
}
}
감사합니다
편집하다
내 뷰가 하나 또는 다른 것을 표시하도록하는 버튼 동작의 토글 변수를 업데이트하는 작업 버전이 있습니다. 작동하지만 애니메이션 효과를 적용 할 수 없으며 SwiftUI에서 "올바른"것처럼 보이지 않습니다. , 내가 모르는 더 나은 것이 있다고 생각합니다.
애니메이션을 추가하고 싶다면 다음을 시도 할 수 있습니다.
struct ContentView: View {
...
@State var showEditView = false
var body: some View {
NavigationView {
TabView(selection: $tabSelection) {
...
view3
.tabItem {
Text("3")
}
.tag(3)
}
.navigationBarItems(leading: navigationBarLeadingItems)
}
}
}
private extension ContentView {
var view3: some View {
VStack {
if showEditView {
FormView()
.background(Color.red)
.transition(.slide)
} else {
Text("View 3")
.background(Color.blue)
.transition(.slide)
}
}
}
}
struct FormView: View {
var body: some View {
Form {
Text("test")
}
}
}
가능한 대안은 ViewRouter 를 사용하는 것입니다 : @EnvironmentObject를 사용하여 SwiftUI에서보기 사이를 탐색하는 방법 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다