Reputation: 311
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. 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?
Upvotes: 6
Views: 5263
Reputation: 794
The answer given by @Meseery is still valid, here is the updated code for Swift 5 on 2023:
let desiredHeight:CGFloat!
if UIDevice.current.userInterfaceIdiom == .phone{
desiredHeight = 259
}else{
if UIDevice.current.orientation == .portrait{
desiredHeight = 260
}else {
desiredHeight = 300
}
}
let heightConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1.0, constant: desiredHeight)
view.addConstraint(heightConstraint)
Upvotes: 0
Reputation: 175
In iOS 9 to UIInputView class was added property allowsSelfSizing
, defaults to NO
. When set to YES
, iOS would not add height constraint, which equals to height of default keyboard. In such case you are responsible for providing sufficient constraints that will determine height (so at least one subview is needed).
Upvotes: 2
Reputation: 1855
Here's my solution as Apple Documentation Suggests :
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.
Upvotes: 9