Francis
Francis

Reputation: 949

Increase and Decrease iOS8 custom keyboard size

I'm making a custom keyboard in iOS8 and I'd like to be able to increase and decrease the size of the keyboard. I know from the iOS documentation that you can increase the height of the keyboard after it has loaded using this code:

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let expandedHeight:CGFloat = 500
let heightConstraint = NSLayoutConstraint(item:self.view,
    attribute: .Height,
    relatedBy: .Equal,
    toItem: nil,
    attribute: .NotAnAttribute,
    multiplier: 0.0,
    constant: expandedHeight)
self.view.addConstraint(heightConstraint)
}

But the problem with this code is, if the expandedHeight is 500 to begin with, if at a later time in the running of the keyboard I want to increase that to 600 for example, nothing happens and sometimes the keyboard crashes.

Is there any other way to increase and decrease the height of the keyboard that works when I increase the height?

Also I've been using a swipe up gesture to increase the height of the keyboard and a swipe down gesture to decrease it. Can you swipe up and down without lifting your finger and have it register as an up and a down gesture?

Upvotes: 2

Views: 909

Answers (1)

lifjoy
lifjoy

Reputation: 2188

Please look at the accepted answer from @skyline75489-- which is the only solution that worked for me.

Like you, my keyboard has a gesture recognizer that toggles the keyboard height between short/tall values. My gesture recognizer calls a method which sets landscapeHeight/portraitHeight to the appropriate values, then calls [self updateViewConstraints] to modify the height constraint.

I'm using two separate gesture recognizers; one for swipe up, another for swipe down. You could replace those with a single pan gesture recognizer which continues to track while the finger is still down, but I don't think you could adjust the height in real time while tracking.

Upvotes: 1

Related Questions