Ios 使用NSLayoutConstraint更改约束

Ios 使用NSLayoutConstraint更改约束,ios,swift,autolayout,xcode8,Ios,Swift,Autolayout,Xcode8,项目的Github 例如,我改变了这一点: let orangeViewCenterXConstraint = NSLayoutConstraint( item: orangeView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0.0 ) 为此: orangeView.centerXAnchor.cons

项目的Github

例如,我改变了这一点:

let orangeViewCenterXConstraint = NSLayoutConstraint(
  item: orangeView,
  attribute: .centerX,
  relatedBy: .equal,
  toItem: view,
  attribute: .centerX,
  multiplier: 1.0,
  constant: 0.0
)
为此:

orangeView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
如何正确处理以下问题:

let purpleViewBottomSpaceConstraint = NSLayoutConstraint(
  item: purpleView,
  attribute: .bottom,
  relatedBy: .equal,
  toItem: orangeView,
  attribute: .top,
  multiplier: 1.0,
  constant: -8.0
)
我试过:

purpleView.bottomAnchor.constraint(equalTo: <#T##NSLayoutAnchor<AnyObject>#>, constant: <#T##CGFloat#>)
purpleView.bottomAnchor.constraint(等式:,常数:)
但是我认为
purpleView
需要在
purpleView
orangeView
之间留出一个空间,所以从技术上讲
equalTo:orangeView
常量:-8.0
,但这是错误的


我不确定我目前正在做什么,所以我来这里学习。

您必须指定
主播,例如:

NSLayoutConstraint.activate([
    purpleView.bottomAnchor.constraint(equalTo: orangeView.topAnchor, constant: -8)
])

谢谢在xcode8中是:
purpleView.bottomAnchor.constraint(equalTo:orangeView.topAnchor,常量:-8)
Yep,在xcode8中,这是新语法。相应地更新了答案。我有另一个问题:
let purpleViewTopSpaceConstraint=NSLayoutConstraint(项目:purpleView,属性:.top,relatedBy:.equal,toItem:self.topLayoutGuide,属性:.bottom、 乘数:1.0,常数:8.0)
Solved:
purpleView.topAnchor.constraint(equalTo:self.topLayoutGuide.bottomAnchor,常数:8.0)])
正确。如果您想要顶部边距,您需要
topLayoutGuide
。问题已解决!,如果您有任何问题,请查看github;)