【问题标题】:NSlayoutConstraint with swift 1.2 error带有swift 1.2错误的NSlayoutConstraint
【发布时间】: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


【解决方案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)

【讨论】:

    【解决方案2】:

    正如错误警告您: 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

    【讨论】:

    • 宽度约束可以有一个 toItem 只是在使用“.NotAnAttribute”作为相关属性时没有。
    • 伙计们,我非常感谢您的帮助...我修改为 nil,但它仍然崩溃..如果我删除所有约束应用程序不会崩溃..需要的是..将视图添加到 UItableview 的 HEADER,并将标签居中放在那里..
    • 到目前为止,唯一适用于 WidthCons 的约束,一旦我取消注释任何其他约束,我就会收到错误.. 我认为它与 toItem: view.superview 相关跨度>
    • 约束不会让你的应用崩溃,只要你遵守规则,如果它崩溃了,你必须调试它或粘贴崩溃日志。但是这类问题很容易修复,因为你可以添加断点来知道哪里出错了。对于自动布局的问题,很难用几句话来解释,我认为你应该先了解自动布局的工作原理,然后再开始编码。使用故事板或 xib 让您对使用自动布局进行热身。
    • 感谢 wngzero,我只需要使用情节提要添加视图到表头并通过那里进行约束。所以我得到了我需要的东西...感谢所有人的回答! ,我最好的问候
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2016-01-22
    • 2015-11-24
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多