Incorrect keyboard height after rotation

I have an iOS custom keyboard that changes height when it rotates.

My code works fine 95% of the time ... But in some cases (see below) the height does not change when turning to landscape - the portrait height remains.

The problem can be reproduced with this (almost) minimal code - create a new keyboard extension target and copy this code to KeyboardViewController:

class KeyboardViewController: UIInputViewController {
    private var orangeView = UIView()
    private var heightConstraint: NSLayoutConstraint!
    @IBOutlet var nextKeyboardButton: UIButton!

    override func updateViewConstraints() {
        super.updateViewConstraints()
        let screenSize = UIScreen.mainScreen().bounds.size
        let screenH = screenSize.height
        let screenW = screenSize.width
        let isLandscape = !(self.view.frame.size.width == screenW * ((screenW < screenH) ? 1 : 0) + screenH * ((screenW > screenH) ? 1 : 0))
        let desiredHeight: CGFloat = isLandscape ? 193 : 252
        if heightConstraint.constant != desiredHeight {
            heightConstraint!.constant = desiredHeight
            orangeView.frame = CGRect(x: 0, y: 0, width: screenW, height: isLandscape ? 193 : 252)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        nextKeyboardButton = UIButton(type: .System)
        nextKeyboardButton.setTitle("Next Keyboard", forState: .Normal)
        nextKeyboardButton.sizeToFit()
        nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
        nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
        heightConstraint = NSLayoutConstraint(item:self.inputView!, attribute:.Height, relatedBy:.Equal, toItem:nil, attribute:.NotAnAttribute, multiplier: 0.0, constant: 0) //preparing heightConstraint
        heightConstraint?.priority = 999
        orangeView.backgroundColor = UIColor.orangeColor()
        view.addSubview(orangeView)
        view.addSubview(self.nextKeyboardButton)
        let nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
        let nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
        view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint])
    }

    override func viewWillAppear(animated: Bool) {
        if self.view.constraints.filter({c in c == self.heightConstraint}).isEmpty {
            self.view.addConstraint(heightConstraint)
        }
        view.setNeedsUpdateConstraints() //ensures that updateViewConstraints always gets called at least once
        super.viewWillAppear(animated)
    }
}

For more on how this works, see this and this answer.

When you start the keyboard, it will probably work fine - it is slightly higher, and after rotation, the orange view covers the entire keyboard:

enter image description here

However, you can also get this (portrait height persists after rotation):

enter image description here

isue ( ):

  • "", .
  • " " ( " " → "" )
  • , .
  • 1-3, ( 2-4 ).

:

  • , , .
  • , -
  • -
  • Debuger , heightConstraint!.constant 193, view.frame.height view.window?.frame.height 253 ( )

:

  • , heightConstraint!.constant = desiredHeight, view,
  • , heightConstraint!.constant ,

? , SwiftKey .

0
1

- iPhone 6+, , iPhone 6 iPhone,

, .

-, ,

iOS 8.0 .

-[UIViewController viewWillAppear:], . -[UIViewController viewDidAppear:], , , .

, , , .

, SwiftKey (-) .

+1

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


All Articles