Extension for iOS 8 applications Custom Keyboard - change the height for orientation

Changing the custom keyboard height is just as per the documentation . Here is a quick equivalent version of the code shown in the Apple documentation

override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let constraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: 400.0) self.view.addConstraint(constraint) } 

This works, but it changes the height to a CONSTANT value, which is undesirable if you change the orientation of the device. Setting the keyboard height to 400.0 can be fine in portrait mode, but most likely it will not be suitable in landscape mode. For example: The standard keyboard size for iPhone 5 is 320,216 in portrait and 568,162 in landscape. Setting a constant height changes the size of the keyboard view to 320,400 in the portrait and 568,400 (which is actually the entire screen) in the landscape.

My only thought now is to create a dictionary containing the size of the keyboard representation for each type of device and each orientation, and then update the constant height constraint every time the device changes orientation. Has anyone found a more elegant solution?

+5
source share
1 answer

I did this by implementing willAnimateRotationToInterfaceOrientation: in the keyboard controller. Theoretically, we should use the new willTransitionToTraitCollection:withTransitionCoordinator: or, preferably, viewWillTransitionToSize:withTransitionCoordinator: but they don't seem to be called on UIInputViewControllers, at least from 8.0.2. So for now, the best option is to keep a reference to your heightConstraint and change the constant in your willAnimateRotation implementation.

In addition, I remember that there was a little something similar to the fact that we figured out a new orientation; I ended up doing something like:

let isLandscape: Bool = UIInterfaceOrientationIsLandscape(toInterfaceOrientation) and then using this.

+2
source

Source: https://habr.com/ru/post/1203085/


All Articles