可搜索无法保持焦点并显示键盘
我有以下代码:
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] 无法使用上下文更新焦点
然后,我尝试点击搜索字段,控制台显示以下内容:
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] 推迟焦点更新到项目
有趣的是,相同的代码也适用于 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:
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 技术交流群。

就我而言,我通过添加以下内容解决了这个问题:
它稍微改变了搜索栏的外观,但它对我有用。
In my case, I solved it by adding this:
It changes search bar's appearance a little, but it works for me.