2016-01-09 7 views
0

По какой-то причине я получаю terminating with uncaught exception of type NSException с помощью этого кода. Я просто пытаюсь сделать поставить ограничения autolayout на вид TabBar:Swift 2.0 ограничения автоопределения программно

func addCommentView() { 
    let commentView = UIView.loadFromNibNamed("AddCommentView") 
    view.addSubview(commentView!) 

    let leftConstraint = NSLayoutConstraint(item: commentView!, attribute: NSLayoutAttribute.Leading, relatedBy: .Equal, toItem: view, 
    attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0) 

    let rightConstraint = NSLayoutConstraint(item: commentView!, attribute: NSLayoutAttribute.Trailing, relatedBy: .Equal, toItem: view, 
    attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0) 

    let bottomConstraint = NSLayoutConstraint(item: commentView!, attribute: NSLayoutAttribute.Bottom, relatedBy: .Equal, toItem: view, 
    attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0) 

    let heightConstraint = NSLayoutConstraint(item: commentView!, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, 
    multiplier: 1.0, constant: 0) 

    commentView!.addConstraints([leftConstraint, rightConstraint, bottomConstraint, heightConstraint]) 

} 
+1

Похоже, вы устанавливаете высоту комментария на 0. Это то, что вы хотите? –

ответ

1

Вы должны отредактировать:

commentView!.addConstraints([leftConstraint, rightConstraint, bottomConstraint, heightConstraint]) 

к:

view.addConstraints([leftConstraint, rightConstraint, bottomConstraint, heightConstraint]) 

Краш будет идти. Но как насчет ограничения высоты?

+0

Хорошо, я добавил высоту 50 и изменил ее на 'view.addConstraints ... 'и добавил строку' commentView! .translatesAutoresizingMaskIntoConstraints = false'. Теперь он прекрасно работает – user3904534

+0

@ user3904534 поздравление вам: D –

Смежные вопросы