0

I'm coding a custom keyboard and i want to customize the height.. Apple Documentation give that code only in Objective-C, does anyone know how to write it in Swift Language? This is the code from Apple:

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];

I tried to write it like this but it doesn't do anything..:

override func viewDidAppear(animated:Bool) {
    super.viewDidAppear(true)

   let nib = UINib(nibName: "KeyboardView", bundle: nil)
    let objects = nib.instantiateWithOwner(self, options: nil)
    view = objects[0] as UIView;

    let _viewHeight: CGFloat = 256

    let const1 = NSLayoutConstraint(
        item:self.view, attribute:.Height,
        relatedBy:.Equal, toItem:nil,
        attribute:.NotAnAttribute,multiplier:0, constant: _viewHeight)

    view.addConstraint(const1)

 } 

Help me please!

Francesco
  • 169
  • 1
  • 1
  • 14
  • Check out this question: https://stackoverflow.com/questions/24167909/ios-8-custom-keyboard-changing-the-height/25819565#25819565 – ljk321 Jan 24 '15 at 04:26

1 Answers1

1

You have a multiplier:0, it should be multiplier:1.0.

You might also mixed self.view and view = objects[0] as UIView. You should add a constraint to your main self.view which is = self.inputView and add your custom view on it.

let customView = objects[0] as UIView
customView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubView(customView)

//layout
let left = NSLayoutConstraint(item: customView, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1.0, constant: 0.0)
let top = NSLayoutConstraint(item: customView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: 0.0)
let right = NSLayoutConstraint(item: customView, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: 0.0)
let bottom = NSLayoutConstraint(item: customView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([left, top, right, bottom])

let const1 = NSLayoutConstraint(
    item:self.view, attribute:.Height,
    relatedBy:.Equal, toItem:nil,
    attribute:.NotAnAttribute,multiplier:1.0, constant: _viewHeight)
self.view.addConstraint(const1)
SoftDesigner
  • 5,640
  • 3
  • 58
  • 47