简体   繁体   English

如何更改iOS自定义键盘的高度?

[英]How do I change height of iOS custom keyboard?

I've been working on an iOS custom keyboard (in an .xib file), but I haven't been able to change the height within storyboard. 我一直在研究iOS自定义键盘(在.xib文件中),但我无法在故事板中更改高度。 Is there a way to change the height of the keyboard within storyboard or do I have to find a way to do it programmatically? 有没有办法在故事板中更改键盘的高度,还是我必须找到一种以编程方式进行操作的方法?

Here's my solution as Apple Documentation Suggests : 这是Apple文档建议的解决方案:

You can adjust the height of your custom keyboard's primary view using Auto Layout. 您可以使用“自动布局”调整自定义键盘主视图的高度。 By default, a custom keyboard is sized to match the system keyboard, according to screen size and device orientation. 默认情况下,根据屏幕大小和设备方向,自定义键盘的大小可与系统键盘匹配。 A custom keyboard's width is always set by the system to equal the current screen width. 系统始终将自定义键盘的宽度设置为等于当前屏幕宽度。 To adjust a custom keyboard's height, change its primary view's height constraint. 要调整自定义键盘的高度,请更改其主视图的高度约束。

 override func viewWillAppear(animated: Bool) {
    let desiredHeight:CGFloat!
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone{
            desiredHeight = 259
    }else{
        if UIDevice.currentDevice().orientation == .Portrait{
            desiredHeight = 260
        }else {
            desiredHeight = 300
        }
    }
    let heightConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: desiredHeight)
    view.addConstraint(heightConstraint)

}

documentation said ,you can adjust your keyboard height after being layout ,so you can use viewWillAppear to do so. 文档说,你可以在布局后调整你的键盘高度,这样你就可以使用viewWillAppear来做到这一点。

You're gonna want to check out this site. 你想要查看这个网站。 It provides a lot of information! 它提供了很多信息! custom keyboard 自定义键盘

In iOS 9 to UIInputView class was added property allowsSelfSizing , defaults to NO . 在iOS 9中向UIInputView类添加了属性allowsSelfSizing ,默认为NO When set to YES , iOS would not add height constraint, which equals to height of default keyboard. 设置为YES ,iOS不会添加高度约束,这等于默认键盘的高度。 In such case you are responsible for providing sufficient constraints that will determine height (so at least one subview is needed). 在这种情况下,您负责提供足够的约束来确定高度(因此至少需要一个子视图)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM