如何相对于父视图的边距将 AddConstraint 添加到按钮,使其看起来距上边距 30 度,距右边距 30 度
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
这是我正在尝试的代码:
@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 点处。请帮忙。
如果界面生成器是一个选项,那么界面生成器非常适合此类功能。 Select 您要为其添加边距并在顶部和右侧设置固定边距的对象。
这是一个屏幕截图。
这是我正在尝试的代码:
@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 点处。请帮忙。
如果界面生成器是一个选项,那么界面生成器非常适合此类功能。 Select 您要为其添加边距并在顶部和右侧设置固定边距的对象。
这是一个屏幕截图。