4

I have added a view called myView in interface builder as can be seen in the code below and have added this to the main view as a subview, I have then added three buttons programatically. I have added these as subviews to the main view before trying to apply anchor constraints. I have also set the buttons to translatesAutoresizingMaskIntoConstraints = false. Does anyone one know what i am doing wrong here to be getting the following error?

Unable to activate constraint with anchors because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.

    @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"]

    }
Marcus Levi
  • 65
  • 1
  • 6
  • 7
    There is too much code here to see what’s going wrong easily. Essentially what the error means is that you haven’t added a view as a sub view before adding a constraint. You must add the views as sub views before adding constraints. – Fogmeister Mar 31 '18 at 16:26
  • Which line is the error happening on? You have 5 or 6 different views here. – Fogmeister Mar 31 '18 at 16:27
  • Also... your width anchors should not be using `self.view.frame.width`. You should be making the width equal to `view.widthAchor` – Fogmeister Mar 31 '18 at 16:29
  • 1
    @Fogmeister I have deleted some code to make it clearer, all remaining code left is essential. I have also tried view.widthAchor however get the following failure ...Cannot convert value of type 'NSLayoutDimension' to expected argument type 'CGFloat' – Marcus Levi Mar 31 '18 at 16:37
  • ok cool. Which line is the error bappening? I can’t see anything wrong with the code. – Fogmeister Mar 31 '18 at 16:39
  • Vehement.dropDownView:0x15f0319c0.top"> and – Marcus Levi Mar 31 '18 at 16:41
  • Add an exception breakpoint. Which line of your code is causing the error. – Fogmeister Mar 31 '18 at 16:42
  • That width constant thing. Yeah change `equalToConstant` to `equalTo`. – Fogmeister Mar 31 '18 at 16:59
  • 1
    This line: `myStretchesButton.centerYAnchor.constraint(equalTo: self.myButton.dropView.bottomAnchor, ....)` Is `dropView` a subview of `myButton`? – Code Different Mar 31 '18 at 17:46
  • As already said, the actual issue is with your view hierarchy. I looked through what you call essential code and the *single* thing referenced that *is not* (possibly) in the view hierarchy is as @Code_Different noticed - `dropView`. There are a few ways to find out what your view hierarchy *actually* is - please try them. –  Mar 31 '18 at 17:58
  • Thanks all - I will try and implement the changes suggested and will take a look at my dropView as there is too much code to post on here. Much appreciated – Marcus Levi Mar 31 '18 at 18:03
  • Why are you adding myView as a subview when you have an outlet to it so it is already in the view hierarchy? – vacawama Mar 31 '18 at 18:08

0 Answers0