2015-01-11 44 views
0

我正在编写自定义键盘,我想自定义高度.. Apple文档仅在Objective-C中给出该代码,是否有人知道如何使用Swift语言编写它?这是苹果公司代码:在swift中加载视图后自定义customKeyboard高度

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

我试图把它写这样,但它不会做任何事情..:

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) 

} 

请帮助我!

+0

看看这个问题:https://stackoverflow.com/questions/24167909/ios-8-custom-keyboard-changing-the-height/25819565#25819565 – skyline75489

回答

1

你有一个multiplier:0,它应该是multiplier:1.0

你也可能混合self.viewview = objects[0] as UIView。您应该添加约束到您的主self.view这是= self.inputView并添加您的自定义视图。

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) 
相关问题