应用程序启动后,在Swiftui的Textfield上禁用自动对焦
MacOS 12将Textfield设置为发射时的第一响应者几秒钟,然后再进行更改。
尝试调用下面的方法,diDishlaunching,willfinishlaunching,init()主应用程序的init(),
NSApp.keyWindow?.makeFirstResponder(nil)
NSApp.keyWindow?.resignFirstResponder()
在该应用程序自动删除焦点之前,短暂延迟了几秒钟。
如何防止这种行为?我有一些动画,当Textfield集中时会触发。
我
enum Focus { case some, none }
@FocusState var focus : Focus
正在观察到,启动焦点设置为某些,然后将其设置为无
macOS 12 sets the TextField as first responder on launch for a few seconds before this changes.
Tried calling the methods below onAppear, didFinishLaunching, willFinishLaunching, init() of the main app
NSApp.keyWindow?.makeFirstResponder(nil)
NSApp.keyWindow?.resignFirstResponder()
There is a brief delay of a few seconds before the app automatically removes focus.
How to prevent this behaviour ? I have some animations which are triggered when the TextField is focused.
I'm using
enum Focus { case some, none }
@FocusState var focus : Focus
I've observed that on launch focus is set to some and then to nil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这确实看起来像一个错误(如果需要,请向Apple提出反馈)。
发现的解决方法是禁用启动的临时控制(禁用> Textfield 无法获得焦点)并在下一个事件周期中重新确定。
用Xcode 13.3/MacOS测试12.2
这是主要部分:
项目中的完整代码在这里
This really looks like a bug (file a feedback to Apple if you want).
A found workaround is to disable temporary control on launch (disabled
TextField
cannot get focus) and reenable it in next event cycle.Tested with Xcode 13.3 / macOS 12.2
Here is main part:
Complete code in project is here