SwiftUI:在navigationBarItems内部实现ZStack会显示navigationBarItems错误

时间:2020-10-17 01:34:40

标签: swiftui swiftui-list zstack navigationbaritems swiftui-zstack

我正在尝试在implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0 implementation 'androidx.test.espresso:espresso-idling-resource:3.2.0' // Android Testing Support Libraries's runner and rules androidTestUtil 'androidx.test:orchestrator:1.3.0' androidTestImplementation 'androidx.test:rules:1.3.0' // Espresso UI Testing androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0' // Android Instrumentation Tests wth JUnit 5 testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.0" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.7.0" testImplementation "org.junit.jupiter:junit-jupiter-params:5.7.0" androidTestImplementation "org.junit.jupiter:junit-jupiter-api:5.7.0" androidTestImplementation "org.junit.jupiter:junit-jupiter-engine:5.7.0" androidTestImplementation "org.junit.jupiter:junit-jupiter-params:5.7.0" androidTestImplementation "de.mannodermaus.junit5:android-test-core:1.2.0" androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:1.2.0" 内实现ZStack来加载自定义警报。这是我的实现:

navigationBarItems

在这一行上,我收到此错误:

enter image description here

你们中的任何人都知道为什么我会收到此警告吗?还是可以解决此错误?

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您不能将SwiftUI视图置于关闭状态-这是没有意义的,视图应处于视图层次结构中,按钮可以激活状态以使用视图进行操作,例如

var body: some View {
    ZStack {
        ItemsAlert(isShown: $isPresented, text: $text)
        
        VStack{
           List(self.itemsStore.names){ item in
               Text("hello")
           }
        }
    }

    
    .navigationBarItems(trailing: Button(action: {
       self.isPresented = true    // << activate state            
    }, label: {
        Image(systemName: "plus")
    }))
}