I'm making a custom keyboard with an UIInputViewController.
I want to display a xib layout inside a rect. Right now, I determine the correct size of the CGRect, in which I place my xib layout, from UIScreen.main.bounds
. However, UIScrren.main.bounds.width
is longer than the safe area width when I rotate the iPhone 11 to landscape orientation. As a result, my rect is too wide and part of it is not displayed.
iphone11 screen
How can I determine safe bounds? I would like to replace UIScreen.main.bounds
with something like UIScreen.main.safeArea
but it seems like I have to do something more complicated than that.
Should I just use UIScreen.main.nativeBounds
? If so, I'm not sure how to convert from pixels to points.
First of all if you are detecting the height I'm not sure what you will need since you don't use the max height in a keyboard view. But for detecting the max width, which seems to be the issue in your linked screen cap, try the following:
Instead of using
UIScreen.main.bounds
to detect the width useself.view.bounds.size.width
. This seems to be set by the OS and will reflect the maximum width you can use after it gets set by the OS. When first displaying the screen, the earliest I can detect this value is inviewWillLayoutSubviews()
. In the case that the screen is rotated, the earliest I can detect the new size, after rotation, is in theviewLayoutMarginsDidChange()
event.Since the
viewLayoutMarginsDidChange()
event is also fired when the view is displayed you could use that event for both circumstances.