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

问题描述

我正在尝试在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

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

非常感谢您的帮助

解决方法

您不能将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")
    }))
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...