7

根据文档,更改自定义键盘的高度很简单。这是 Apple 文档中显示的 swift 等效版本的代码

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let constraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: 400.0)
    self.view.addConstraint(constraint)
}

这确实有效,但它会将高度更改为 CONSTANT 值,如果您更改设备的方向,这是不可取的。将键盘高度设置为 400.0 在纵向模式下可能很好,但在横向模式下可能不合适。例如:iPhone 5 的标准键盘视图大小为纵向 320,216 和横向 568,162。设置一个恒定的高度会将键盘视图大小更改为纵向 320,400 和横向 568,400(实际上是整个屏幕)。

我现在唯一的想法是为每种设备类型和每个方向创建一个包含键盘视图大小的字典,然后在每次设备更改方向时更新恒定高度约束。有没有人找到更优雅的解决方案?

4

1 回答 1

2

我通过willAnimateRotationToInterfaceOrientation:在键盘的视图控制器中实现了这一点。理论上我们应该使用新的 willTransitionToTraitCollection:withTransitionCoordinator:或者理想情况下可能viewWillTransitionToSize:withTransitionCoordinator:的,但这些似乎并没有在 UIInputViewControllers 上被调用,至少从 8.0.2 开始。因此,目前最好的选择似乎是保留对您的 heightConstraint 的引用,并在您的 willAnimateRotation.

我还记得在实际找出新方向时有一点问题。我最终做了类似的事情:

let isLandscape: Bool = UIInterfaceOrientationIsLandscape(toInterfaceOrientation)然后使用它。

于 2014-10-19T18:01:50.607 回答