我在界面构建器中添加了一个名为myView的视图,如下面的代码所示,并将其作为子视图添加到主视图中,然后以编程方式添加了三个按钮。在尝试应用锚点约束之前,我已将这些作为子视图添加到主视图中。我还设置了按钮translatesAutoresizingMaskIntoConstraints = false。有没有人知道我在这里做错了以获得以下错误?
无法使用锚点激活约束,因为它们没有共同的祖先。约束或其锚点是否引用不同视图层次结构中的项目?这是非法的。
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
//Warmup button
view.addSubview(myView)
myButton = dropDownButton.init(frame: CGRect(x: 0,y: 0, width: 0, height: 0))
self.view.addSubview(myButton)
myButton.translatesAutoresizingMaskIntoConstraints = false
myButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
myButton.centerYAnchor.constraint(equalTo: self.myView.topAnchor, constant: 38).isActive = true
myButton.widthAnchor.constraint(equalToConstant: self.view.frame.width).isActive = true
myButton.heightAnchor.constraint(equalToConstant: 76).isActive = true
myButton.dropView.dropDownOptions = ["Chest Expansions", "Side Arm Raises", "Dives", "Raised Arm Circles", "Overhead Punches", "Punches"]
//stretches button
myStretchesButton = dropDownButton.init(frame: CGRect(x: 0,y: 0, width: 0, height: 0))
myStretchesButton.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(myStretchesButton)
myStretchesButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
myStretchesButton.centerYAnchor.constraint(equalTo: self.myButton.dropView.bottomAnchor, constant: 38).isActive = true
myStretchesButton.widthAnchor.constraint(equalToConstant: self.view.frame.width).isActive = true
myStretchesButton.heightAnchor.constraint(equalToConstant: 76).isActive = true
myStretchesButton.dropView.dropDownOptions = ["Chest Expansions", "Side Arm Raises", "Dives", "Raised Arm Circles", "Overhead Punches", "Punches"]
}