【问题标题】:How to set constraints on Horizontal line programmatically in Swift? [closed]如何在 Swift 中以编程方式在水平线上设置约束? [关闭]
【发布时间】:2017-12-04 06:28:27
【问题描述】:

如何在 Swift 中以编程方式在水平线上设置约束? 我在“再试一次”按钮中设置了这一行。所以,请告诉我如何在这条线上设置约束。

代码是:

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

hrzLine.backgroundColor = UIColor.white

self.view.addSubview(hrzLine)

enter image description here

【问题讨论】:

  • 请说明您要达到的目标....即使您附加的图像也不清楚。你想达到和图片一样的效果吗??
  • 你说的是哪条水平线?请更清楚。
  • 上面的白色水平线保持在屏幕底部。

标签: ios swift interface-builder ios-autolayout


【解决方案1】:

相应地更改常量的值。并确保在 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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多