旋转后不正确的键盘扩展高度

我有iOS自定义键盘旋转时改变高度。

我的代码可以正常工作95%…但是在某些情况下(见下文),当旋转到横向时高度不会改变 – 纵向高度保持不变。

问题可以用这个(几乎)最小的代码来重现 – 创build新的键盘扩展目标并将这个代码复制到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) } } 

有关如何工作的更多信息,请参阅本文和此答案。

当你运行键盘时,它可能会正常工作 – 它稍微高一点,旋转之后橙色视图覆盖整个键盘:

在这里输入图像说明

但是,你也可能会得到这个(旋转后保留肖像高度):

在这里输入图像说明

重现isue的步骤(使用上面的代码):

  1. 在信息应用程序中打开键盘,旋转横向并返回
  2. 如果你没有看到消息应用程序的消息(按主页button2x – >向上滑动消息)
  3. 再次打开消息,旋转横向并返回
  4. 您可能需要重复步骤1-3几次才能看到问题(通常不超过2-4次)

我知道的:

  • 如果你看到这个问题,你会一直坚持下去,直到键盘被隐藏并重新显示
  • 如果你看到问题隐藏并重新显示键盘与相同的应用程序 – 问题总是消失
  • 只有在杀死并重新启动托pipe应用后,该问题才会再次出现
  • Debuger显示heightConstraint!.constant193view.frame.heightview.window?.frame.height仍然是253 (直接更改框架不能修复它)

我曾经尝试过:

  • 而不是只设置约束heightConstraint!.constant = desiredHeight首先从view删除它,设置新的值,然后重新添加它
  • 我确认了heightConstraint!.constant总是被改变的

如何解决或解决此问题? 必须有一个解决scheme,因为SwiftKey没有这个问题。

我遇到了一个键盘扩展的类似问题 – 在iPhone 6+型号上, 一次在应用程序中调用键盘时,无法正确设置其高度,偶尔也会在iPhone 6和其他iPhone型号上正确设置高度。

我最终find了一个解决scheme,虽然它有它自己的缺点。

首先,文件说明

在iOS 8.0中,您可以随时调整自定义键盘的高度,使其初始视图最初在屏幕上绘制。

你正在设置高度约束-[UIViewController viewWillAppear:] ,在严格的文档解释下,为时尚早。 如果你把它设置在-[UIViewController viewDidAppear:] ,你现在遇到的问题应该被解决。

但是,您可能会发现,您的高度调整是在视觉上震动,因为它出现在键盘出现之后。

我不知道SwiftKey如何在键盘出现之前设置它们的高度(看似), 避免这个问题。