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

查看:157
本文介绍了导航栏中的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

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

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天全站免登陆