如何在Swift中使用NSLayoutConstraint在UITextField中添加填充?

0 uitextfield ios nslayoutconstraint swift

我有一个子类UIView,我有,UITextField并希望移动占位符文本一点点.我NSLayoutConstraint用于设置子视图的位置和大小.我已创建UIView并添加它leftView,UITextField但它崩溃说明"视图层次结构没有为约束做好准备".以下是我的代码:

class MasterView: UIView {

let searchTextField = UITextField()
let paddingView = UIView()

override init(frame: CGRect) {
    super.init(frame: frame)

    searchTextField.placeholder = "Search videos"
    searchTextField.translatesAutoresizingMaskIntoConstraints = false
    paddingView.translatesAutoresizingMaskIntoConstraints = false
    searchTextField.leftView = paddingView
    searchTextField.leftViewMode = UITextFieldViewMode.Always

    self.addSubview(searchTextField)

}

// Gets called when using storyboard

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
    self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.TopMargin, multiplier: 1.0, constant: 20))

    self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: 0.12, constant: 0))

    self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))

    self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))

    self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1.0, constant: 0))

    self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.TopMargin, multiplier: 1.0, constant: 0))

    self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.Width, multiplier: 0.12, constant: 0))

    self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0))

}
Run Code Online (Sandbox Code Playgroud)

}

Gig*_*ggs 9

这个问题的正确解决方案:

@IBDesignable
class FormTextField: UITextField {

  @IBInspectable var paddingLeft: CGFloat = 0
  @IBInspectable var paddingRight: CGFloat = 0

  override func textRect(forBounds bounds: CGRect) -> CGRect {
    return CGRect(x: bounds.origin.x + paddingLeft, y: bounds.origin.y, width: bounds.size.width - paddingLeft - paddingRight, height: bounds.size.height)
  }

  override func editingRect(forBounds bounds: CGRect) -> CGRect {
    return textRect(forBounds: bounds)
  }
}
Run Code Online (Sandbox Code Playgroud)

取自这里