Francis
Francis

Reputation: 949

How to change iOS 8 custom keyboard height with Swift?

I'm making a custom keyboard for iOS 8 using Swift. I'm extremely new to iOS development and I've been unable to solve this problem on my own. In the App Extension Programming documents online Apple said that you can change the height of any custom keyboard after it has been launched. They provided an example in Objective-C but I'm using Swift and so far, I can't seem to find a Swift version of the code.

Can someone convert the 3 lines of code into Swift or show me a way to change the height of the keyboard please?

Here's the webpage with the code: https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/Keyboard.html

And here's the lines of code:

CGFloat _expandedHeight = 500;
NSLayoutConstraint *_heightConstraint = 
[NSLayoutConstraint constraintWithItem: self.view 
    attribute: NSLayoutAttributeHeight 
    relatedBy: NSLayoutRelationEqual 
    toItem: nil 
    attribute: NSLayoutAttributeNotAnAttribute 
    multiplier: 0.0 
    constant: _expandedHeight];
[self.view addConstraint: _heightConstraint];

Upvotes: 2

Views: 3821

Answers (3)

Francis
Francis

Reputation: 949

I added the code provided to the viewDidAppear method and the keyboard resized itself.

Here's the code that worked.

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)
}

Upvotes: 4

codeIgnitor
codeIgnitor

Reputation: 765

completely,

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)

Upvotes: 0

Mundi
Mundi

Reputation: 80271

let heightConstraint = NSLayoutConstraint(item: view, 
                                     attribute: .Height, 
                                     relatedBy: .Equal, 
                                        toItem: nil, 
                                     attribute: .NotAnAttribute, 
                                    multiplier: 0.0, 
                                      constant: expandedHeight)

Upvotes: 1

Related Questions