Hey guys, I wrote a function which creates an UIView with UIImageView and some Label in it.
This is my Code:
The problem is at the topAnchor of the address Label, it throws me this error: "Thread 1: "Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x60000344ea40 \"UILabel:0x7f9198d6e830.top\"> and <NSLayoutYAxisAnchor:0x60000344e7c0 \"UILabel:0x7f9198d6e5c0.bottom\"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.""
I really do not understand where the problem is, can somebody help me? :)
This is my Code:
Code Block func setUpInserateWindows(images: [UIImage], priceText: String, descriptionText: String, streetAddress: String, cityAddress: String) { var inserates = [UIView]() for image in images { let inserateView = UIView() let imageView = UIImageView() let price = UILabel() let description = UILabel() let address = UILabel() let city = UILabel() inserateView.addSubview(imageView) inserateView.addSubview(price) inserateView.addSubview(description) //InserateView inserateView.sizeToFit() inserateView.backgroundColor = .clear inserateView.translatesAutoresizingMaskIntoConstraints = false inserateView.widthAnchor.constraint(equalToConstant: 190).isActive = true inserateView.heightAnchor.constraint(equalToConstant: 100).isActive = true //ImageView imageView.backgroundColor = .systemBlue imageView.image = image imageView.contentMode = .scaleAspectFill imageView.layer.cornerRadius = 20 imageView.layer.masksToBounds = true imageView.translatesAutoresizingMaskIntoConstraints = false imageView.centerXAnchor.constraint(equalTo: inserateView.centerXAnchor).isActive = true imageView.topAnchor.constraint(equalTo: inserateView.topAnchor, constant: 5).isActive = true imageView.widthAnchor.constraint(equalToConstant: 260).isActive = true imageView.heightAnchor.constraint(equalToConstant: 130).isActive = true //Pricetag price.text = priceText price.textAlignment = .left price.textColor = .black price.font = UIFont.boldSystemFont(ofSize: 20) price.translatesAutoresizingMaskIntoConstraints = false price.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 5).isActive = true price.leadingAnchor.constraint(equalTo: imageView.leadingAnchor).isActive = true price.widthAnchor.constraint(equalToConstant: 150).isActive = true price.heightAnchor.constraint(equalToConstant: 30).isActive = true //Description description.text = descriptionText description.numberOfLines = 1 description.textColor = .systemGray description.textAlignment = .left description.font = UIFont.boldSystemFont(ofSize: 15) description.translatesAutoresizingMaskIntoConstraints = false description.topAnchor.constraint(equalTo: price.bottomAnchor).isActive = true description.leadingAnchor.constraint(equalTo: imageView.leadingAnchor).isActive = true description.widthAnchor.constraint(equalToConstant: 250).isActive = true description.heightAnchor.constraint(equalToConstant: 20).isActive = true //Street Address address.text = streetAddress address.numberOfLines = 1 address.textColor = .systemGray address.textAlignment = .left address.font = UIFont.boldSystemFont(ofSize: 15) address.translatesAutoresizingMaskIntoConstraints = false address.topAnchor.constraint(equalTo: description.bottomAnchor).isActive = true address.leadingAnchor.constraint(equalTo: imageView.leadingAnchor).isActive = true address.widthAnchor.constraint(equalToConstant: 250).isActive = true address.heightAnchor.constraint(equalToConstant: 20).isActive = true inserates.append(inserateView) } for inserate in inserates { stackView.addArrangedSubview(inserate) } }
The problem is at the topAnchor of the address Label, it throws me this error: "Thread 1: "Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x60000344ea40 \"UILabel:0x7f9198d6e830.top\"> and <NSLayoutYAxisAnchor:0x60000344e7c0 \"UILabel:0x7f9198d6e5c0.bottom\"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.""
I really do not understand where the problem is, can somebody help me? :)