emerog
emerog

Reputation: 79

Missing constraints IOS 11 swift 4

I'm trying to add a constraints between two controls: TextField and Separator. But I can't see separator. What is wrong in this code ?

func setupTextField() {
    textField =  UITextField(frame: CGRect(x: 0, y: 0, width: 97, height: 30))
    textField!.backgroundColor = .clear
    textField!.placeholder = placeHolder

    self.addSubview(textField!)

    //MARK: Constraints

    textField!.translatesAutoresizingMaskIntoConstraints = false;
    textField!.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    textField!.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
    textField!.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
}

func setupSeparator() {
    separator = UIView(frame: CGRect(x: 0, y: 32, width: 97, height: 1))
    separator?.backgroundColor = .lightGray

    self.addSubview(separator!)

    separator!.translatesAutoresizingMaskIntoConstraints = false;
    separator!.topAnchor.constraint(equalTo: textField!.bottomAnchor, constant: 1).isActive = true
    separator!.leftAnchor.constraint(equalTo: textField!.leftAnchor).isActive = true
    separator!.rightAnchor.constraint(equalTo: textField!.rightAnchor).isActive = true
}

Upvotes: 1

Views: 231

Answers (1)

TylerP
TylerP

Reputation: 9829

You need to add a height constraint to your separator or else it will get squished down to a height of zero, which is why you can't see it. Add something like the following to your setupSeparator method:

separator!.heightAnchor.constraint(equalToConstant: 1).isActive = true

Upvotes: 2

Related Questions