根据文档,更改自定义键盘的高度很简单。这是Apple文档中显示的等效代码版本
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(实际上是整个屏幕)。
我现在唯一的想法是创建一个字典,其中包含每种设备类型和每个方向的键盘视图大小,然后在每次设备更改方向时更新恒定的高度约束。有没有人找到更优雅的解决方案?
我是通过willAnimateRotationToInterfaceOrientation:
在键盘的视图控制器中实现来实现的。从理论上讲,我们应该使用new, willTransitionToTraitCollection:withTransitionCoordinator:
或者理想情况下使用viewWillTransitionToSize:withTransitionCoordinator:
,但至少从8.0.2版本开始,这些似乎没有在UIInputViewControllers上被调用。因此,目前最好的选择似乎是保留对heightConstraint的引用,并在实现中修改常量willAnimateRotation
。
我还记得,实际上要弄清楚新的方向有些困惑。我最终做了类似的事情:
let isLandscape: Bool = UIInterfaceOrientationIsLandscape(toInterfaceOrientation)
然后使用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句