swiftswift4iphone-xios-keyboard-extension

change views height in Keyboard extension (iPhone X)


I have following problem: I build a keyboard extension, a alternative keyboard to the system keyboard. In all older iPhone models it works fine, but at the iPhone X it doesn't. My problem is, the height of the keyboard's view is decreasing from 216px (iPhone 8 ,8+ , ...) to 141px (iPhone X). Because lower height, my keyboard buttons are now smaller, to small for good user usability.

I did used a .xib file to create the UI, I add all UI - Items programmatically.

iPhone 8 iPhone X

my Question

Is it posible to get more space (height) for the keyboard extension view (specially for the iPhone X) ?


Solution

  • I solve my problem. I found following paragraph in the Apple documentation:

    You can adjust the height of your custom keyboard’s primary view using Auto Layout. By default, a custom keyboard is sized to match the system keyboard, according to screen size and device orientation. A custom keyboard’s width is always set by the system to equal the current screen width. To adjust a custom keyboard’s height, change its primary view's height constraint.

    The following code lines show how you might define and add such a constraint:

    CGFloat _expandedHeight = 500;
    NSLayoutConstraint *_heightConstraint = 
        [NSLayoutConstraint constraintWithItem: self.view 
                                     attribute: NSLayoutAttributeHeight 
                                     relatedBy: NSLayoutRelationEqual 
                                        toItem: nil 
                                     attribute: NSLayoutAttributeNotAnAttribute 
                                    multiplier: 0.0 
                                      constant: _expandedHeight];
    [self.view addConstraint: _heightConstraint];
    

    Swift 4

    override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            let heightConstraint = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 300)
            self.view.addConstraint(heightConstraint)
        }