导航栏中的 SwiftUI 按钮只能使用一次 [英] SwiftUI button in navigation bar only works once

查看:15
本文介绍了导航栏中的 SwiftUI 按钮只能使用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将按钮向上移动到导航栏中只会在附加了工作表时触发一次.该按钮用于显示搜索窗口,但一旦关闭弹出窗口,该按钮将保持非活动状态.

Moving a button up into the navigation bar only triggers once when it has a sheet attached. The button is used to show a search window but once the popup is closed the button remains inactive.

附加的代码是我尝试过的简化版本.起初我在主要部分使用了一个按钮来激活搜索窗口,但我认为导航栏会占用更少的空间.激活有效,但在那种情况下我无法停用它.

The attached code is a simplified version of what I tried. At first I used a button in the main part to activate the search window but I thought that the navigation bar would take less space. The activation worked but in that case I couldn't deactivate it.

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

我希望这两个按钮的行为应该相同.两个放大镜都应激活搜索窗口,清除按钮应将其停用,准备好进行新的尝试,但导航栏中的按钮似乎没有看到显示搜索的变化.

I expect that the two buttons should behave the same. Both magnifying glasses should activate the search window and the clear button should deactivate it ready for a new attempt but it appears that button in the navigation bar isn't seeing the change in showingSearch.

推荐答案

这是一个已知的与导航栏项目相关的错误,不只是工作表,它似乎影响任何模式,我在 IB 中遇到过使用模态转场时相同.

It's a known bug related to navigation bar items and not relegated to just sheets, it seems to affect any modal, and I've encountered it in IB just the same when using modal segues.

不幸的是,这个问题在 11.3 版本中仍然存在,希望他们能尽快解决这个问题.

Unfortunately this issue is still present in 11.3 build, hopefully they get this fixed soon.

这篇关于导航栏中的 SwiftUI 按钮只能使用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆