【问题标题】:How to AddConstraint to a button relative to parent view's margin so that it appears 30 from top margin and 30 from right margin如何将约束添加到相对于父视图边距的按钮,使其显示为距上边距 30 和距右边距 30
【发布时间】:2017-07-03 20:12:15
【问题描述】:

这是我正在尝试的代码:

    @IBOutlet weak var btnInfo: UIButton!

override func viewWillAppear(_ animated: Bool) {
    self.view.addConstraint(NSLayoutConstraint(item: self.view,
                                             attribute: .topMargin,
                                             relatedBy: .equal,
                                             toItem: btnInfo,
                                             attribute: .notAnAttribute,
                                             multiplier: 1,
                                             constant: 20))
    self.view.addConstraint(NSLayoutConstraint(item: self.view,
                                             attribute: .rightMargin,
                                             relatedBy: .equal,
                                             toItem: btnInfo,
                                             attribute: .notAnAttribute,
                                             multiplier: 1,
                                             constant: 30))
}

尝试添加.topMargin.rightMargin 约束以使btnInfo 出现距顶部30 点和距右边距30 点。请帮忙。

【问题讨论】:

  • 使用锚点!它会让你的生活更轻松。见here

标签: ios swift swift3 nslayoutconstraint


【解决方案1】:

如果界面构建器是一个选项,界面构建器非常适合这种类型的功能。选择要添加边距的对象,并在顶部和右侧设置恒定边距。

这是一个屏幕截图。

【讨论】:

  • 非常感谢。您的回答帮助了我,解决了我的问题。
  • 总是乐于提供帮助!
猜你喜欢
  • 1970-01-01
  • 2015-05-11
  • 2014-02-27
  • 2011-08-10
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多