旋转后不正确的键盘扩展高度
问题描述:
我有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)
}
}
当您运行键盘它可能会很好地工作 - 这是略高与旋转后的橙色视图覆盖整个键盘:
然而,你也可能得到这个(纵向高度旋转后保持):
步骤来重现isue(与上面的代码):
- 打开键盘消息应用程序中,旋转景观和背部
- 如果您没有看到这个问题杀消息应用程序(按Home键2倍 - >刷卡的消息了)
- 开放消息再次,旋转景观和背部
- 您可能需要重复步骤1-3几次看问题(通常不超过2-4次)
我知道什么:
- 如果你看到的问题,您会继续seing,直到键盘被隐藏和重新显示
- 如果你看到问题隐藏和重新显示键盘withing相同的应用程序 - 这个问题总是在前看不见
- 问题可能只杀死并重新启动托管应用程序
- Debuger表明
heightConstraint!.constant
是193
后再次出现,但两者view.frame.height
和view.window?.frame.height
仍然253
(改变帧直接不能解决问题)
我曾尝试:
- 而不是只需设置约束
heightConstraint!.constant = desiredHeight
首先从view
删除,设置新的值,然后重新添加 - 我验证了
heightConstraint!.constant
总是改变当它应该是
如何解决或解决此问题?必须有一个解决方案,因为SwiftKey没有这个问题。
答
我遇到类似的问题与键盘扩展 - 在iPhone 6+机型,它总是未能正确设置它的高度上旋转的第一一次键盘在一个应用程序调用,偶尔上iPhone 6和其他iPhone机型。
我终于找到了一个解决方案,虽然它有它自己的缺点。
首先,文档指出
在iOS中8.0,你可以调整自定义键盘的高度后的任何时间其主要观点最初绘制在屏幕上。
您正在设置高度限制-[UIViewController viewWillAppear:]
,根据对文档的严格解释,这太早了。如果您将其设置为-[UIViewController viewDidAppear:]
,则应解决您当前遇到的问题。
但是,您可能会发现,您的高度调整是视觉上的震撼,因为它出现在键盘出现之后。
我不知道如何SwiftKey设置其高度(似乎)之前,键盘出现和避免此问题。
谢谢!看起来像在'viewDidAppear'中添加约束来解决这个问题。至于导致视觉刺激身高的问题:基于我在iOS 9上的测试,应用程序中的第一次出现总是OK,无论在viewWillAppear或viewDidAppear中是否添加了约束,任何后续事件都会产生冲突。在iOS 8上有差异,但不是那么大......你有不同的经验吗? – drasto