4

我在界面生成器中添加了一个名为 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"]

    }
4

0 回答 0