繁体   English   中英

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

[英]How to add padding inside UITextField using NSLayoutConstraint in Swift?

我有一个UIView的子类,其中有UITextField ,想稍微移开占位符文本。 我正在使用NSLayoutConstraint设置子视图的位置和大小。 我创建了UIView并将其添加为UITextField leftView ,但崩溃,提示“视图层次结构未为约束准备”。 下面是我的代码:

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))

}

}

正确解决此问题的方法:

@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)
  }
}

这里

使UITextField为自定义类,并在该类中实现这两个方法,并使您的文本字段为自定义文本字段。

class customTextField: UITextField {

 override func textRectForBounds(bounds: CGRect) -> CGRect {
  return super.textRectForBounds(UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 5, 0, 0)))
 }

 override func editingRectForBounds(bounds: CGRect) -> CGRect {
  return super.editingRectForBounds(UIEdgeInsetsInsetRect(bounds,  UIEdgeInsetsMake(0, 5, 0, 0)))
 }
}
let paddingForFirst = UIView(frame: CGRectMake(0, 0, 5, self.Email_Txt.frame.size.height-5))
Email_Txt.leftView = paddingForFirst
Email_Txt.leftViewMode = UITextFieldViewMode .Always

试试这个代码将对您有帮助

override func viewDidLoad() {
super.viewDidLoad()
let paddingView = UIView(frame: CGRectMake(0, 0, 15, self.myTextField.frame.height))
myTextField.leftView = paddingView
myTextField.leftViewMode = UITextFieldViewMode.Always
}

只需在下面写: -SWIFT 2.1.1

let tempView = UIView(frame:CGRectMake(6, 0, 22, 26))
tempView.backgroundColor = UIColor.clearColor()
searchTextField.leftView?.frame = CGRectMake(0, 0, tempView.frame.size.width, tempView.frame.height)

searchTextField.leftViewMode = UITextFieldViewMode.Always
searchTextField.leftView = tempView

yon不应设置或限制该填充视图。 删除填充视图的所有约束。 它是textfield的左视图,因此它会根据文本字段的约束自动进行调整。 并设置填充视图框架,以确保您期望填充的空间。

希望这会有所帮助:)

您遇到此错误“未为约束准备视图层次结构”,因为paddingView被添加为左视图/右视图,并且不需要任何约束。

注意:不需要对作为左视图/右视图添加到UITextField视图施加约束。 因此,您只需要设置paddingView框架并将其分配给leftView 无需对paddingView约束。

暂无
暂无

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

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