【发布时间】: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)
}
}
有关其工作原理的更多信息,请参阅 this 和 this 答案。
当您运行键盘时,它可能会正常工作 - 它会稍高一些,旋转后橙色视图会覆盖整个键盘:
但是你也可能得到这个(人像高度在旋转后保持):
重现问题的步骤(上面的代码):
- 在消息应用中打开键盘,横向和向后旋转
- 如果您没有看到终止消息应用程序的问题(按主页按钮 2 次 -> 向上滑动消息)
- 再次打开邮件,横向和向后旋转
- 您可能需要重复步骤 1-3 几次才能看到问题(通常不超过 2-4 次)
我所知道的:
- 如果您看到该问题,您会一直看到它,直到键盘被隐藏并重新显示
- 如果您看到问题隐藏并使用同一应用重新显示键盘 - 问题总是会消失
- 只有在杀死并重新启动托管应用后,该问题才会再次出现
- 调试器显示
heightConstraint!.constant是193但view.frame.height和view.window?.frame.height仍然是253(直接换帧不能修复)
我尝试过的:
- 不只是设置约束
heightConstraint!.constant = desiredHeight首先将其从view中删除,设置新值然后重新添加它 - 我确认
heightConstraint!.constant总是在应该更改的时候更改
如何修复或解决此问题?必须有解决方案,因为 SwiftKey 没有这个问题。
【问题讨论】:
标签: ios iphone swift ios-app-extension custom-keyboard