将按钮向上移动到导航栏中仅当附加了工作表时才会触发一次。该按钮用于显示搜索窗口,但弹出窗口关闭后该按钮将保持非活动状态。
所附代码是我尝试过的简化版本。起初,我在主要部分使用了一个按钮来激活搜索窗口,但我认为导航栏会占用更少的空间。激活有效,但在这种情况下我无法停用它。
import SwiftUI
struct ContentView: View {
@State var showingSearch: Bool = false
var body: some View {
NavigationView {
VStack {
Text("Hello World!")
Button(
action: { self.showingSearch = true },
label: { Image(systemName: "magnifyingglass") }
)
.sheet(
isPresented: $showingSearch,
content: { Search( showingSearch: self.$showingSearch ) }
)
}
.navigationBarItems(
leading: Image(systemName: "square.and.pencil"),
trailing: Button(
action: { self.showingSearch = true },
label: { Image(systemName: "magnifyingglass") }
)
.sheet(
isPresented: $showingSearch,
content: { Search( showingSearch: self.$showingSearch ) }
)
)
}
}
}
struct Search: View {
@Binding var showingSearch: Bool
var body: some View {
NavigationView {
Text("Search")
.navigationBarTitle("Search", displayMode: .inline)
.navigationBarItems(trailing:
Button(
action: { self.showingSearch = false },
label: {Image(systemName: "clear") }
)
)
}
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
Run Code Online (Sandbox Code Playgroud)
我希望这两个按钮的行为应该相同。两个放大镜都应该激活搜索窗口,而清除按钮应该停用它,为新的尝试做好准备,但导航栏中的按钮似乎没有看到显示搜索的变化。
这是一个与导航栏项目相关的已知错误,并且不仅仅局限于工作表,它似乎会影响任何模式,并且我在 IB 中遇到过它,就像使用模式转场时一样。
不幸的是这个问题在 11.3 版本中仍然存在,希望他们很快就能解决这个问题。
归档时间: |
|
查看次数: |
1590 次 |
最近记录: |