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

时间:2016-03-22 03:05:48

标签: ios iphone swift ios-app-extension custom-keyboard

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

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

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

有关其工作原理的详细信息,请参阅thisthis回答。

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

enter image description here

但是你可能也会得到这个(旋转后保持纵向高度):

enter image description here

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

  1. 在消息应用中打开键盘,旋转横向和后退
  2. 如果你没有看到问题杀死消息应用程序(按主页按钮2x - &gt;刷卡消息)
  3. 再次打开邮件,旋转横向和后退
  4. 您可能需要重复步骤1-3几次以查看问题(通常不超过2-4次)
  5. 我所知道的:

    • 如果您发现问题,您将继续观看,直到键盘被隐藏并重新显示
    • 如果您看到问题隐藏并重新显示具有相同应用的键盘 - 问题始终消失
    • 只有在杀死并重新启动托管应用程序后,问题才会再次出现
    • Debuger显示heightConstraint!.constant193view.frame.heightview.window?.frame.height仍然是253(直接更改框架无法修复)

    我尝试了什么:

    • 不是仅设置约束heightConstraint!.constant = desiredHeight,而是首先将其从view中移除,设置新值然后重新添加
    • 我确认heightConstraint!.constant总是应该更改

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

1 个答案:

答案 0 :(得分:1)

我遇到类似的键盘扩展问题 - 在iPhone 6+型号上,总是无法在旋转第一次时键盘时正确设置其高度在应用程序中调用,偶尔在iPhone 6和其他iPhone型号上调用。

我最终找到了一个解决方案,尽管它有其自身的缺点。

首先,文档说明了

  

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

您在-[UIViewController viewWillAppear:]中设置了高度约束,根据对文档的严格解释,这个约束为时过早。如果您在-[UIViewController viewDidAppear:]中进行设置,则应解决您当前遇到的问题。

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

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

相关问题