John Doah
John Doah

Reputation: 1999

Cannot convert value of type '()' to expected element type 'NSLayoutConstraint'

I followed a YouTube tutorial on creating a dropdown list in Swift. I encountered an error which I can't fix, I looked here in Stackoverflow and on search results from Google but nothing worked for me. This is the problem I get:

Cannot convert value of type '()' to expected element type 'NSLayoutConstraint'

This is the code:

class Dropdown: UIButton, DropDownProtocol {
    var dropView = DropDownView()
    var height = NSLayoutConstraint()
    var isOpen = false

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.appBlack
        dropView = DropDownView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        dropView.translatesAutoresizingMaskIntoConstraints = false
        dropView.delegate = self
    }

    override func didMoveToSuperview() {
        self.superview?.addSubview(dropView)
        self.superview?.bringSubviewToFront(dropView) //Needed?

        NSLayoutConstraint.activate([
            dropView.topAnchor.constraint(equalTo: self.bottomAnchor),
            dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
            dropView.widthAnchor.constraint(equalTo: self.widthAnchor),
            height = (dropView.heightAnchor.constraint(equalToConstant: 0)) <--- problem occurs here
        ])
    }
}

Upvotes: 0

Views: 1590

Answers (1)

rmaddy
rmaddy

Reputation: 318774

Don't try to assign a value to height inside the call to activate.

Refactor just a bit:

override func didMoveToSuperview() {
    self.superview?.addSubview(dropView)
    self.superview?.bringSubviewToFront(dropView) //Needed?

    height = dropView.heightAnchor.constraint(equalToConstant: 0)

    NSLayoutConstraint.activate([
        dropView.topAnchor.constraint(equalTo: self.bottomAnchor),
        dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
        dropView.widthAnchor.constraint(equalTo: self.widthAnchor),
        height
    ])
}

Upvotes: 2

Related Questions