当键盘被解散时,Swiftui表将自己关闭

发布于 2025-01-24 16:30:22 字数 1793 浏览 4 评论 0原文

问题

时,当代码显示↓时,当“显示表格”按钮被挖掘出来时会显示一张表。 该表仅包含一个文本编辑器。 当解散文本编辑器的键盘(按键盘中的右下按钮)时,键盘就会被取消,并且表也被删除。

我希望床单不会被解雇。也许这是Swiftui的一个错误...

请帮助我弄清楚为什么纸张被驳回以及如何预防。

我正在使用iPad Pro iOS 15.4.1。 该问题不会在模拟器上发生。

struct ContentView: View {

    @State var showSheet = false

    @State var input = ""

    var body: some View {
        Form {
            // Section 1
            Section {
                Text("1")
                Text("1")
                Text("1")
                Text("1")
                Text("1")
            }
            // Section 2
            Section {
                Text("1")
                Text("1")
                Text("1")
                Text("1")
                Text("1")
            }
            // Section 3
            Section {
                Text("1")
                Text("1")
                Text("1")
                Text("1")
                Text("1")
            }
            // Section 4
            Section {
                Button {
                    showSheet = true
                } label: {
                    Text("show sheet")
                }
                .sheet(isPresented: $showSheet, onDismiss: {
                    print("dismissed")
                }) {
                    TextEditor(text: $input)
                }
            }
            // Section 5
//            Section {
//                Text("1")
//                Text("1")
//                Text("1")
//                Text("1")
//                Text("1")
//            }
        }
    }
}

图片

单击此处查看gif

我尝试了什么

  • 如果删除第1或2部分或3节, ,如果不注意第5节,问题不会发生
  • ,则该问题不会出现
  • 全屏幕中存在相同的问题

Problem

As the code shows↓, a sheet is presented when "show sheet" button in Form is tapped.
The sheet contains only a TextEditor.
When keyboard of the texteditor is dismissed (press the right bottom button in the keyboard), keyboard is dismissed, and the sheet is also dismissed.

I hope the sheet wouldn't be dismissed. Maybe it's a bug of SwiftUI...

Please help me figure out why the sheet is dismissed and how to prevent it.

I'm using iPad Pro iOS 15.4.1.
The problem doesn't occur on a simulator.

struct ContentView: View {

    @State var showSheet = false

    @State var input = ""

    var body: some View {
        Form {
            // Section 1
            Section {
                Text("1")
                Text("1")
                Text("1")
                Text("1")
                Text("1")
            }
            // Section 2
            Section {
                Text("1")
                Text("1")
                Text("1")
                Text("1")
                Text("1")
            }
            // Section 3
            Section {
                Text("1")
                Text("1")
                Text("1")
                Text("1")
                Text("1")
            }
            // Section 4
            Section {
                Button {
                    showSheet = true
                } label: {
                    Text("show sheet")
                }
                .sheet(isPresented: $showSheet, onDismiss: {
                    print("dismissed")
                }) {
                    TextEditor(text: $input)
                }
            }
            // Section 5
//            Section {
//                Text("1")
//                Text("1")
//                Text("1")
//                Text("1")
//                Text("1")
//            }
        }
    }
}

Picture

click here to see the gif

What I've tried

  • if remove Section 1 or 2 or 3, the problem doesn't occur
  • if uncomment Section 5, the problem doesn't occur
  • fullScreenCover has the same problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年九夏 2025-01-31 16:30:22

尝试将.sheet(...)放在表格外,对我有用。

try putting the .sheet(...) outside the Form, it works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文