2017-12-17 8 views
1

私は2つのコントロール:TextFieldとSeparatorの間に制約を追加しようとしています。しかし、私はセパレータを見ることができません。このコードで何が間違っていますか?制約がありませんIOS 11 swift 4

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 
} 

答えて

2

高さの制約をセパレータに追加する必要があります。そうでないと、高さがゼロになることがあります。そのため、見えません。 setupSeparatorメソッドに次のようなコードを追加します。

separator!.heightAnchor.constraint(equalToConstant: 1).isActive = true 
関連する問題