Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8 Custom Keyboard App Extension - Change height for orientation

Changing your custom keyboard's height is simple according to documentation. Here is the swift-equivalent version of code shown in Apple's 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 does work, but it changes the height to a CONSTANT value which is not desirable if you change the device's orientation. Setting the keyboard height to 400.0 might be fine in portrait mode but it likely will not be appropriate in landscape mode. For example: The standard keyboard view size for an iPhone 5 is 320,216 in portrait and 568,162 in landscape. Setting a constant height changes keyboard view size to 320,400 in portrait and 568,400 (which is effectively the entire screen) in landscape.

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

like image 607
Jon Avatar asked Sep 21 '14 18:09

Jon


1 Answers

I have done this by implementing willAnimateRotationToInterfaceOrientation: in my keyboard's view controller. Theoretically we should be using the new willTransitionToTraitCollection:withTransitionCoordinator: or ideally maybe viewWillTransitionToSize:withTransitionCoordinator:, but those do not seem to get called on UIInputViewControllers, at least as of 8.0.2. So currently the best option seems to be to keep a reference to your heightConstraint, and to modify the constant in your implementation of willAnimateRotation.

Also I remember there was a bit of a gotcha with actually figuring out the new orientation; I ended up doing something like:

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

like image 171
cmyr Avatar answered Oct 04 '22 09:10

cmyr