ios - 如何相对于父 View 的边距将 AddConstraint 添加到按钮,使其看起来距上边距 30 度,距右边距 30 度

标签 ios swift swift3 nslayoutconstraint

这是我正在尝试的代码:

    @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 点处。请帮忙。

最佳答案

如果界面生成器是一个选项,那么界面生成器非常适合此类功能。选择您要添加边距的对象,并在顶部和右侧设置固定边距。

这是一个屏幕截图。 enter image description here

关于ios - 如何相对于父 View 的边距将 AddConstraint 添加到按钮,使其看起来距上边距 30 度,距右边距 30 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44893272/

相关文章:

ios - 完全缩放图像而不丢失其内容

ios - 如何将 iOS App 商店的两个应用程序分支合并为一个?

ios - NSDate 造成内存泄漏

ios - UISearchBar 和 UISearchBarController 之间的区别?

ios - 如何以编程方式触发UIButton Action

swift - 为什么我的@lazy 属性会崩溃,但如果我将其设置为非惰性属性,它就可以正常工作?

ios - 无法将此 JSON 转换为字符串

ios - Viewcontroller 在实现 rootviewcontroller 后只显示黑屏

iOS 输入麦克风输出蓝牙设备

swift - 不止一次使用 swift 3 migrator