iosswiftipadios-keyboard-extension

Custom keyboard displays in wrong location in iOS 11


We have a custom keyboard that's been working for years. It was created in iOS8 when they first came out and re-written for Swift in iOS 9. It's built programmatically, no nib involved. On iPad devices with iOS 11 installed it's showing up about 75px too high. The entire keyboard is present and functional, but there's a gray bar below it. I have an iPad with 10.3.2 and it works just fine. On the simulator with iPad air 2, and two physical devices we have with iOS 11 it displays too high. Originally it didn't have any constraints at all. I added a constraint for height but that didn't do any good.

override func viewDidLoad() {
    ...
    let size = self.orientationUtil.getSizeForCurrentOrientation()
    heightConstraint = NSLayoutConstraint(item: self.inputView as Any,
                                  attribute: NSLayoutAttribute.height,
                                  relatedBy: NSLayoutRelation.equal,
                                  toItem: nil,
                                  attribute: NSLayoutAttribute.notAnAttribute,
                                  multiplier: 1.0,
                                  constant: size.height)
   ...
}

override func updateViewConstraints() {
    super.updateViewConstraints()

    if (self.view.frame.size.width == 0 || self.view.frame.size.height == 0) {
        return
    }
    let size = self.orientationUtil.getSizeForCurrentOrientation()
    inputView?.removeConstraint(heightConstraint!)
    heightConstraint!.constant = size.height
    inputView?.addConstraint(heightConstraint!)
}

Should the code here be sufficient to ensure the keyboard is on the bottom of the host view? Should providing the correct height constraint be enough for the system to know where it should be displayed? I thought about trying a bottom constraint but I'm not sure what I would use as the toItem: argument. Has anyone else noticed their keyboard showing up incorrectly on iPad with iOS 11?

TIA, Mike


Solution

  • I found out that the contents of the keyboard need to be constrained to the bottom of the view. In my case our keyboard (a UIView) is in self.keyboard.

    self.keyboard.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true self.keyboard.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

    The keyboard settled on the bottom after I added these two lines.

    Mike