带有swift 1.2错误的NSlayoutConstraint

时间:2015-08-10 01:40:13

标签: swift xcode6 nslayoutconstraint

我的代码曾经在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

2 个答案:

答案 0 :(得分:1)

您的第一个约束(noteBodyLabel上的宽度限制)是错误的...... toItem应为零

  var widthCons = NSLayoutConstraint(
        item: noteBodyLabel,
        attribute: .Width,
        relatedBy: .Equal,
        toItem: nil,
        attribute: .NotAnAttribute, // this means to toItem should be nil
        multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 80)

答案 1 :(得分:0)

错误警告你: 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'

.NotAnAttribute的宽度约束不应包含toItem个对象。试试toItem:nil