可搜索无法保持焦点并显示键盘

发布于 01-16 22:02 字数 4868 浏览 4 评论 0原文

我有以下代码:

struct FocusStateTestView: View {
    
    @State var showSheet = false
    
    var body: some View {
        VStack {
    
            Text("FocusStateTestView")
            
            Button {
                showSheet.toggle()
            } label: {
                Text("present sheet")
            }

            
        }
        .sheet(isPresented: $showSheet) {
            SheetTestView()
        }
    }
}


struct SheetTestView: View {
    
    @State var text = ""
    var body: some View {
        
        NavigationView {
            
            Form {
                
                Text("hello")
                
            }
            .searchable(text: $text)
        }
        
    }
    
}

该表显示正常:

在此处输入图像描述

问题:

键盘不显示。我无法在该文本搜索文本字段中输入内容。

显示工作表时,控制台会记录以下内容:

2022-03-24 15:40:11.230674-0600 myApp[92118:2113819] [UIFocus] 无法使用上下文更新焦点。没有可用的附加信息。 2022-03-24 15:40:13.455689-0600 myApp[92118:2113819] [UIFocus] 推迟焦点更新到项目。没有可用的附加信息。 2022-03-24 15:40:13.455920-0600 myApp[92118:2113819] [UIFocus] 无法使用上下文更新焦点。没有可用的其他信息。

然后,我尝试点击搜索字段,控制台显示以下内容:

objc[92118]:Class _PathPoint 在两者中均实现/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x12b85f338 ) 和/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x1395d4fe8 )。将使用两者之一。哪一个是未定义的。 objc[92118]:类 _PointQueue 在 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/ 中实现库/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x12b85f310)和/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework /TextInputUI (0x1395d5010)。将使用两者之一。哪一个是未定义的。 2022-03-24 15:40:26.005733-0600 myApp[92118:2113819] [UIFocus] 推迟焦点更新到项目。没有可用的附加信息。 2022-03-24 15:40:26.005824-0600 myApp[92118:2113819] [UIFocus] 无法使用上下文更新焦点。无其他可用信息。

有趣的是,相同的代码也适用于 iPhone。在 iPhone 上,我仍然收到一些关于它的奇怪日志:

objc[92218]: Class _PathPoint is Implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x12aff7338 ) 和/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x13f380fe8 )。将使用两者之一。哪一个是未定义的。 objc[92218]:类 _PointQueue 在 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/ 中实现库/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x12aff7310) 和/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x13f381010 )。将使用两者之一。哪一个是未定义的。 2022-03-24 15:43:25.454520-0600 myApp[92218:2116972] [HardwareKeyboard] -[UIApplication getKeyboardDevicePropertiesForSenderID:shouldUpdate:usingSyntheticEvent:],无法获取 senderID 的设备属性 (778835616971358211) 使用主要而是键盘信息。 2022-03-24 15:43:25.457774-0600 myApp[92218:2116972] [HardwareKeyboard] -[UIApplication getKeyboardDevicePropertiesForSenderID:shouldUpdate:usingSyntheticEvent:],无法获取 senderID 的设备属性 (778835616971358211) 使用主要而是键盘信息。

但至少在 iPhone 上它可以工作。

我该如何解决这个问题?如何在 iPad 上正确显示键盘并进行搜索?

我在模拟器和设备上都有同样的问题。运行 Xcode 版本 13.3 (13E113)。

I have the following code:

struct FocusStateTestView: View {
    
    @State var showSheet = false
    
    var body: some View {
        VStack {
    
            Text("FocusStateTestView")
            
            Button {
                showSheet.toggle()
            } label: {
                Text("present sheet")
            }

            
        }
        .sheet(isPresented: $showSheet) {
            SheetTestView()
        }
    }
}


struct SheetTestView: View {
    
    @State var text = ""
    var body: some View {
        
        NavigationView {
            
            Form {
                
                Text("hello")
                
            }
            .searchable(text: $text)
        }
        
    }
    
}

The sheet presents fine:

enter image description here

Problem:

keyboard does not show up. I cannot type in that text search textfield.

When the sheet is presented the console logs this:

2022-03-24 15:40:11.230674-0600 myApp[92118:2113819] [UIFocus] Failed to update focus with context <UIFocusUpdateContext: 0x6000026bc8c0: previouslyFocusedItem=(null), nextFocusedItem=(null), focusHeading=None>. No additional info available. 2022-03-24 15:40:13.455689-0600 myApp[92118:2113819] [UIFocus] Deferring focus update to item <UISearchBarTextField: 0x14c89da00>. No additional info available. 2022-03-24 15:40:13.455920-0600 myApp[92118:2113819] [UIFocus] Failed to update focus with context <UIFocusUpdateContext: 0x6000026b7660: previouslyFocusedItem=(null), nextFocusedItem=(null), focusHeading=None>. No additional info available.

I then try to tap on the search field, and the console shows this:

objc[92118]: Class _PathPoint is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x12b85f338) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x1395d4fe8). One of the two will be used. Which one is undefined. objc[92118]: Class _PointQueue is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x12b85f310) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x1395d5010). One of the two will be used. Which one is undefined. 2022-03-24 15:40:26.005733-0600 myApp[92118:2113819] [UIFocus] Deferring focus update to item <UISearchBarTextField: 0x14c89da00>. No additional info available. 2022-03-24 15:40:26.005824-0600 myApp[92118:2113819] [UIFocus] Failed to update focus with context <UIFocusUpdateContext: 0x60000269d9a0: previouslyFocusedItem=(null), nextFocusedItem=(null), focusHeading=None>. No additional info available.

Interestingly enough, the same code works on iPhone. I still get some weird logs about it when on iPhone:

objc[92218]: Class _PathPoint is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x12aff7338) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x13f380fe8). One of the two will be used. Which one is undefined. objc[92218]: Class _PointQueue is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x12aff7310) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x13f381010). One of the two will be used. Which one is undefined. 2022-03-24 15:43:25.454520-0600 myApp[92218:2116972] [HardwareKeyboard] -[UIApplication getKeyboardDevicePropertiesForSenderID:shouldUpdate:usingSyntheticEvent:], failed to fetch device property for senderID (778835616971358211) use primary keyboard info instead. 2022-03-24 15:43:25.457774-0600 myApp[92218:2116972] [HardwareKeyboard] -[UIApplication getKeyboardDevicePropertiesForSenderID:shouldUpdate:usingSyntheticEvent:], failed to fetch device property for senderID (778835616971358211) use primary keyboard info instead.

But on iPhone at least it works.

How can I fix this? How can I properly present the keyboard on the iPad and search??

I have the same problem both on simulator and device. running Xcode Version 13.3 (13E113).

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

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

发布评论

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

评论(1

妳是的陽光2025-01-23 22:02:53

就我而言,我通过添加以下内容解决了这个问题:

.searchable(text: $text, placement: .navigationBarDrawer(displayMode: .always))

它稍微改变了搜索栏的外观,但它对我有用。

In my case, I solved it by adding this:

.searchable(text: $text, placement: .navigationBarDrawer(displayMode: .always))

It changes search bar's appearance a little, but it works for me.

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