繁体   English   中英

缺少约束IOS 11 Swift 4

[英]Missing constraints IOS 11 swift 4

我试图在两个控件之间添加一个约束:TextField和Separator。 但是我看不到分隔符。 此代码有什么问题?

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
}

您需要在分隔符上添加一个高度限制,否则它将被压缩到零高度,这就是为什么看不到它的原因。 将以下内容添加到setupSeparator方法中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM