Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Keyboard in Xcode Preview

Tags:

xcode

swiftui

how can I enable keyboard when tapping a Textfield in Xcode preview. I somehow activated it and clicked some keys and then I accidentally typed a shortcut on my Mac and it disappeared.

How can I enable it again?

like image 586
SwiftNewling Avatar asked Dec 04 '25 13:12

SwiftNewling


2 Answers

It turns out this setting is (very subtly) tied to the "Connect Hardware Keyboard" preference in the iOS Simulator app (found in the menu bar as I/O → Keyboard → Connect Hardware Keyboard). The keyboard will only appear in "Live Preview" mode and with this setting disabled, and Xcode will instantly reset the setting as soon as it detects any direct keyboard input while the preview has focus (and the Simulator app will not reflect this reset, so to do it again you'll have to toggle it twice). Worse, flipping the setting in the Simulator app is not enough by itself; you have to force the preview to fully reload the underlying simulator it's using. The easiest way to do this is to restart Xcode or to switch to a different simulated device (such as from iPhone 11 to iPhone 12). You can also try executing this command in a terminal:

xcrun simctl --set ~/Library/Developer/Xcode/UserData/Previews/Simulator\ Devices shutdown all

This command will not affect any simulators running in the iOS Simulator app, but will cause all previews to stop working until Xcode relaunches the simulated devices, and may cause Xcode's UI to temporarily behave strangely (or at least the effects were temporary when I tried it in Xcode 12.4).

Once the setting is in effect, focusing on a text input field will cause the software keyboard area to appear. The area is responsive to clicks and all the keys work, but the keyboard itself does not seem to render. It is obviously not intended that this works, and it may stop working at any time in the future.

like image 125
Gwynne Raskind Avatar answered Dec 07 '25 17:12

Gwynne Raskind


This might be a bug, I tried running preview in debug mode in several cases and noticed that when a text field is tapped, console throws errors about some constraints that are not satisfied and there's no access to them.

I also enabled "Show View Frames" from Xcode's "Debug" menu, the keyboard frame actually appears on the screen but is not shown for some reason. In rare cases, the constraints error was gone for no reason and the keyboard was successfully displayed but simply restarting the preview caused it to disappear again. So this might be a bug and if so, hope this gets fixed in next releases.

like image 45
M Reza Avatar answered Dec 07 '25 16:12

M Reza