如何在视图和顶部布局指南之间找到约束?

时间:2016-08-18 10:39:36

标签: ios swift autolayout

我不想在视图和顶部布局指南之间制作约束的IBOutlet。我也不想放置标识符。我想以编程方式找到它。任何人请帮助我。

2 个答案:

答案 0 :(得分:1)

选择constraint并添加identifier' TopLayOut'如下图所示:

enter image description here

并在您的代码中迭代约束并找到正确的约束:

 for constraint in self.view.constraints{

            if constraint == "TopLayOut"{
                print("matches")
                break;
            }
  }

答案 1 :(得分:0)

这是你必须以编程方式声明约束的方法。请找到代码段。

let view = UIView()    
let leadingConstraint = view.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor)
let trailingConstraint = view.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor)
let topConstraint = view.topAnchor.constraintEqualToAnchor(self.view.topAnchor)
let bottomConstraint = view.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor, constant: 0)
var setConstraints = [NSLayoutConstraint]()
setConstraints.appendContentsOf([leadingConstraint,trailingConstraint,topConstraint,bottomConstraint])    
NSLayoutConstraint.activateConstraints(setConstraints)

请勾选如果它有帮助。

感谢。