2015-01-11 3 views
0

Я кодирую пользовательскую клавиатуру, и я хочу настроить высоту. Apple Documentation дает этот код только в Objective-C, знает ли кто-нибудь, как его записать в Swift Language ? Это код от Apple:Настроить customKeyboard height после загрузки представления в swift

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.view и view = 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)