【发布时间】:2015-08-10 01:40:12
【问题描述】:
我的代码曾经在 swift 1.1 中工作,但现在在 1.2 中它会因此错误而崩溃。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specified in pairs'
/* Create View with label top header TABLE */
let header:UIView = UIView(frame: CGRectMake(0,0, UIScreen.mainScreen().bounds.width, 40))
var noteBodyLabel = UILabel()
noteBodyLabel.text = "Aquí te presentamos las últimas tendencias en Alacarta.do"
noteBodyLabel.numberOfLines = 2
noteBodyLabel.textColor = UIColor.lightGrayColor()
noteBodyLabel.textAlignment = .Center
noteBodyLabel.font = UIFont(name:noteBodyLabel.font.fontName, size: 12)
noteBodyLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
var widthCons = NSLayoutConstraint(
item: noteBodyLabel,
attribute: .Width,
relatedBy: .Equal,
toItem: view.superview,
attribute: .NotAnAttribute,
multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 80)
let topCons = NSLayoutConstraint(
item: noteBodyLabel,
attribute: .Top,
relatedBy: .Equal,
toItem: view.superview,
attribute: .Bottom,
multiplier: 1, constant: 10);
/*
let bottomCons = NSLayoutConstraint(
item: noteBodyLabel,
attribute: .Bottom,
relatedBy: .Equal,
toItem: ,
attribute: .Bottom,
multiplier: 1, constant: 0);
*/
let leftCons = NSLayoutConstraint(
item: noteBodyLabel,
attribute: .Left,
relatedBy: .Equal,
toItem: view.superview,
attribute: .Left,
multiplier: 1, constant: 40);
let rightCons = NSLayoutConstraint(
item: noteBodyLabel,
attribute: .Right,
relatedBy: .Equal,
toItem: view.superview,
attribute: .Right,
multiplier: 1, constant: 40);
header.addSubview(noteBodyLabel)
noteBodyLabel.addConstraints([topCons,leftCons,rightCons,widthCons])
self.tableView.tableHeaderView = header
【问题讨论】:
-
阅读错误!它可以准确地告诉您问题所在。
标签: swift xcode6 nslayoutconstraint