Ios 如何在Swift中以编程方式设置水平线上的约束?

Ios 如何在Swift中以编程方式设置水平线上的约束?,ios,swift,interface-builder,ios-autolayout,Ios,Swift,Interface Builder,Ios Autolayout,如何在Swift中以编程方式设置水平线上的约束? 我在“重试”按钮中设置了此行。那么,请告诉我如何在这条线上设置约束 代码是: let hrzLine = UIView(frame: CGRect(x: 0, y: 647, width: 265, height: 1)) hrzLine.backgroundColor = UIColor.white self.view.addSubview(hrzLine) 相应地更改常量的值。并确保在view.addSubview之前声明hrzline

如何在Swift中以编程方式设置水平线上的约束? 我在“重试”按钮中设置了此行。那么,请告诉我如何在这条线上设置约束

代码是:

let hrzLine = UIView(frame: CGRect(x: 0, y: 647, width: 265, height: 1))

hrzLine.backgroundColor = UIColor.white

self.view.addSubview(hrzLine)

相应地更改常量的值。并确保在view.addSubview之前声明hrzline.translatesAutoresizingMaskIntoConstraints,就像我在下面所做的那样

   let hrzline = UIView()
   hrzLine.backgroundColor = UIColor.white

    hrzline.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(hrzLine)


    self.view.addConstraint(NSLayoutConstraint(item: hrzline, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: hrzline, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: 265))
    self.view.addConstraint(NSLayoutConstraint(item: hrzline, attribute: .top, relatedBy: .equal, toItem: yourTryAgainButton, attribute: .top, multiplier: 1, constant: 10))
    self.view.addConstraint(NSLayoutConstraint(item: hrzline, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 10))

请详细说明您正试图实现的目标…即使您附加的图像也不清楚。你想得到与图片中相同的结果吗?你说的是哪条水平线?请更清楚。上面的白色水平线保持在屏幕底部。