我开始在textfield
以编程方式启动之前应用图标,但主要关注的是在图标之前应用填充。
因为它只是触及文本区域的边界。
我编程如下:
override func viewWillAppear(animated: Bool) {
// Email Icon on left of email Text Field
let imageView1 = UIImageView()
let image1 = UIImage(named: "Message.png")
imageView1.image = image1
imageView1.frame = CGRectMake(100, 0, 10, 10)
emailTextField.leftView = imageView1
emailTextField.leftViewMode = UITextFieldViewMode.Always
// Password Icon on left of pass Text Field
let imageView2 = UIImageView()
let image2 = UIImage(named: "Lock.png")
imageView2.image = image2
imageView2.frame = CGRectMake(100, 0, 10, 10)
passwordTextField.leftView = imageView2
passwordTextField.leftViewMode = UITextFieldViewMode.Always
}
但是边框和图标之间的所有空间都非常少。
有没有办法在它们之间填充?
如果有任何办法,请告诉我。
答案 0 :(得分:3)
设置 leftImage 框架,因为它与 contenView 的 x (即) contentView对齐10个点的帧是(0,0,25,20)然后将 leftImage 的帧设置为(10,0,15,20)
然后将 leftImage 作为子视图添加到 contentView
现在很容易将 contentView 添加为textView的leftView。
let leftImage = UIImageView()
let image1 = UIImage(named: "Key")
leftImage.image = image1
let contentView = UIView()
contentView.addSubview(leftImage)
contentView.frame = CGRectMake(0, 0, 25, 20)
leftImage.frame = CGRectMake(10, 0, 25, 20)
passwordTextField.leftView = contentView
passwordTextField.leftViewMode = UITextFieldViewMode.Always
Previops输出:
最终输出:
答案 1 :(得分:0)
您可以选择宽度为20的UIView
,然后将leftVeiw
添加到此视图中。现在将此视图设置为leftView
。
答案 2 :(得分:0)
您可以通过继承UITextField
class field: UITextField {
override func leftViewRectForBounds(bounds: CGRect) -> CGRect {
var rect = bounds
rect.origin.x -= 10
return rect
}
override func textRectForBounds(bounds: CGRect) -> CGRect {
var rect = bounds
rect.origin.x -= 10
return rect
}
}
答案 3 :(得分:0)
您只需在一行中执行此操作
textfield.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);