【问题标题】:Incorrect Keyboard Extension height after rotation旋转后键盘扩展高度不正确
【发布时间】:2016-07-08 19:30:03
【问题描述】:

我有 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 答案。

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

但是你也可能得到这个(人像高度在旋转后保持):

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

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

我所知道的:

  • 如果您看到该问题,您会一直看到它,直到键盘被隐藏并重新显示
  • 如果您看到问题隐藏并使用同一应用重新显示键盘 - 问题总是会消失
  • 只有在杀死并重新启动托管应用后,该问题才会再次出现
  • 调试器显示heightConstraint!.constant193view.frame.heightview.window?.frame.height 仍然是253(直接换帧不能修复)

我尝试过的:

  • 不只是设置约束 heightConstraint!.constant = desiredHeight 首先将其从 view 中删除,设置新值然后重新添加它
  • 我确认heightConstraint!.constant 总是在应该更改的时候更改

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

【问题讨论】:

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


    【解决方案1】:

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

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

    首先,文档指出

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

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

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

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

    【讨论】:

    • 谢谢!看起来在viewDidAppear 中添加约束可以解决问题。至于视觉上不和谐的高度调整引起的问题:根据我在 iOS 9 上的测试,应用程序中的第一次出现总是可以的,无论是否在viewWillAppearviewDidAppear 中添加约束,任何后续都是不和谐的。在 iOS 8 上有所不同,但没有那么大……您有不同的体验吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 2014-11-22
    • 2015-01-04
    • 2020-10-13
    • 1970-01-01
    • 2016-04-02
    相关资源
    最近更新 更多