旋转后键盘扩展高度不正确 [英] Incorrect Keyboard Extension height after rotation

查看:57
本文介绍了旋转后键盘扩展高度不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

My code works fine 95% times... But in some cases (see below) the height is not changed when rotated to landscape - portrait height is kept.

此问题(几乎)用最少的代码即可复制-创建新的Keyboard Extension目标并将此代码复制到KeyboardViewController:

Issue can be reproduced with this (almost) minimal code - create 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 info on how it works see this and this answer.

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

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

不过,您也可能会得到此信息(旋转后会保持人像高度):

重现此问题的步骤(带有上面的代码):

  1. 在消息"应用中打开键盘,旋转横向和后退
  2. 如果您没有看到此问题,请杀死消息应用程序(按主页按钮2x->向上滑动消息)
  3. 再次打开邮件,旋转风景并返回
  4. 您可能需要重复1-3几次才能看到问题(通常不超过2-4次)

我所知道的:

  • 如果您看到此问题,将一直关注它,直到隐藏并重新显示键盘为止
  • 如果您发现问题隐藏并使用同一应用重新显示键盘-问题总是消失
  • 仅在终止并重新启动托管应用后,问题才可能再次出现
  • 调试器显示heightConstraint!.constant193,但是view.frame.heightview.window?.frame.height仍然是253(直接更改框架不能解决问题)
  • If you see the issue you will keep seing it until keyboard is hidden and re-displayed
  • If you see the issue hide and re-display the keyboard withing same app - the issue always disapears
  • The issue might appear again only after killing and restarting the hosting app
  • Debuger shows that heightConstraint!.constant is 193 but both view.frame.height and view.window?.frame.height are still 253 (changing frame directly does not fix it)

我尝试过的事情:

  • 不仅仅是设置约束heightConstraint!.constant = desiredHeight,首先将其从view中删除,然后设置新值,然后重新添加
  • 我已验证heightConstraint!.constant始终在应更改的时间进行更改
  • Instead of just setting the constraint heightConstraint!.constant = desiredHeight first remove it from view, set new value and then re-add it
  • I verified that heightConstraint!.constant is always changed when it should be

如何解决或解决此问题?必须要有解决方案,因为SwiftKey没有这个问题.

推荐答案

我遇到了键盘扩展的类似问题-在iPhone 6+机型上,它总是未能正确设置其高度第一次在应用程序中调用键盘时 旋转一次,有时在iPhone 6和其他iPhone机型上也是如此.

I faced a similar issue with a keyboard extension -- on iPhone 6+ models, it always failed to set its height correctly on rotations the first time the keyboard was invoked in an app, and occasionally on iPhone 6 and other iPhone models.

我终于找到了解决方案,尽管它有其自身的缺点.

I eventually found a solution, though it has its own drawbacks.

首先,文档指出

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

In iOS 8.0, you can adjust a custom keyboard’s height any time after its primary view initially draws on screen.

您要在-[UIViewController viewWillAppear:]中设置高度限制,根据文档的严格解释,这还为时过早.如果在-[UIViewController viewDidAppear:]中进行设置,则应该解决当前遇到的问题.

You're setting the height constraint in -[UIViewController viewWillAppear:], which, under a strict interpretation of the documentation, is too early. If you set it in -[UIViewController viewDidAppear:], the problem that you're currently having should be solved.

但是,您可能会发现您的高度调整在视觉上很震撼,就像在键盘出现之后那样.

However, you may then find that your height adjustment is visually jarring, as it is occurring after the keyboard is appearing.

我不知道SwiftKey如何设法(似乎)在键盘出现 之前设置其高度,并避免出现此问题.

I have no idea how SwiftKey manages to set their height (seemingly) before the keyboard appears and avoid this issue.

这篇关于旋转后键盘扩展高度不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆