Interface Builder Keyboard Layout Guide
source link: https://useyourloaf.com/blog/interface-builder-keyboard-layout-guide/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Interface Builder Keyboard Layout Guide
Xcode 14 adds support for keyboard layout guides to Interface Builder.
Apple introduced the keyboard layout guide in iOS 15. By creating constraints with the keyboard guide you build layouts that adapt to the keyboard without listening for keyboard notifications. Unfortunately, it was badly broken when first introduced. It was also not available in Interface Builder.
Apple fixed the bugs in iOS 15.4 and now in Xcode 14 it’s also available in Interface Builder:
You can now enable the keyboard layout guide on a scene’s UIView through the size inspector. Constrain views to the layout guide so they adjust during layout when the keyboard shows on screen.
Let’s see what that means.
Showing the Keyboard Layout Guide
Interface Builder hides the keyboard layout guide by default. To show the keyboard layout guide for a view in Interface Builder:
-
Select the view in the document outline to the left of the Interface Builder canvas:
-
In the size inspector, enable the Keyboard Layout Guide for the view (at the bottom, below the safe area guide):
-
You should then see the Keyboard Layout Guide in the document outline:
Beta Bugs?
When the keyboard is not visible the guide should be at the bottom of the screen with a height matching the safe area insets. On an iPhone 13 pro that makes the guide 34 points high. The top of the keyboard layout guide then matches the bottom of the safe area layout guide.
I’m writing this using the first beta release of Xcode 14 which seems to show the keyboard layout guide at the bottom of the screen with zero height in the Interface Builder canvas:
The keyboard guide has the right height at runtime, but Interface Builder shows it incorrectly. I’m assuming that’s a bug in the Xcode 14 beta that Apple will fix (FB10356441).
Note: I’ve also seen a problem with existing projects where the keyboard guide has a zero frame (FB10375112) which leads to layout warnings in Interface Builder.
Adding Constraints
Control-drag between the guide and a view in the document outline to create constraints with the guide. I’ve added a content view and constrained it to the safe area layout guide on all sides except the bottom where I’ve constrained it to the top of the keyboard layout guide:
Note: As mentioned above, Interface Builder has the wrong size and position for the guide so the content view extends to the bottom of the screen. You need to run it on the simulator to see the correct layout:
Showing the Keyboard
I’ve added a text field to the top and a button to the bottom of the content view. Then with the view controller selected, use the attributes inspector to simulate showing the keyboard:
The Interface Builder canvas shows the keyboard layout guide now covers the on-screen keyboard. The content view moves upward keeping the button visible.
Floating Keyboards
On an iPad you can undock the keyboard and move it around the screen. You can have the keyboard layout guide follow the undocked keyboard by setting the option in the attributes inspector:
Unfortunately this doesn’t seem to do anything in the first beta of Xcode 14 (FB10373958). You’ll need to fall back to setting the property in the view controller:
override func viewDidLoad() {
super.viewDidLoad()
view.keyboardLayoutGuide.followsUndockedKeyboard = true
}
Adaptive Constraints
Using the keyboard layout guide in Interface Builder should be fine, when the bugs are fixed, if the main concern is to avoid the keyboard. When you want to do more you’ll likely want to create your constraints programmatically.
For example, in my original post on the keyboard layout guide I pinned a toolbar to the top of the keyboard. With the keyboard undocked on a split-screen iPad you need to adapt the active constraints depending on the position of the keyboard:
Interface Builder doesn’t have a way to build these adaptive constraints. For that you’ll need to fall back to programmatic constraints.
See Also
For a recap and an example of building adaptive constraints with the keyboard layout guide:
Recommend
-
53
Layoutit: A CSS Grid Layout Interface Builder — A...
-
35
As you may have realized based on my previous post, I’m really into improving my efficiency when it comes to programming and using the keyboard in general. That’s why I got into Vim and that’s why now I’m...
-
5
2020-07-21 — Auto-correct text entered with the wrong keyboard layoutTo enter text in some languages you press a special key combination (e.g. Alt-Shift) to toggle the keyboard layout. The keyboard layout context is typically kept separ...
-
10
Thursday, February 18, 2021 A pre-supplied "custom" keyboard layout for X11 Last year I wrote about how to create a
-
6
How to change the keyboard...
-
6
How to change keyboard layout on the GNOME and Plasma login screens
-
8
Most people accept the QWERTY keyboard layout without any complaint. Indeed, it is the layout typically printed on keyboards in English-speaking countries. But what if QWERTY wasn’t accepted as the de facto standard and the Dvorak keyboard la...
-
8
Prompt for Keyboard-Layout during OOBE
-
2
-
6
Today we are talking about Layout Builder vs Paragraphs. TalkingDrupal.com/327 Topics Nic - Family visiting Stephen - Behat John - Vacation time
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK