简体   繁体   English

在uitextfield中添加图标-iOS Swift

[英]add icon in uitextfield - iOS Swift

I'm trying to add icon in textfield. 我正在尝试在文本字段中添加图标。 This is how my UI looks now with code below 这是我的UI现在的样子 ,下面的代码

Im expecting my UI should look similar to this image 我希望我的UI看起来与此图像相似

// creating view (holderView) to hold icon image(image) and add view to textField (txtEmail) //创建视图(holderView)以保存图标image(image)并将视图添加到textField(txtEmail)

    let holderView = UIView(frame: CGRect(x:0, y:0, width: 40, height: 40))

    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))

    let image = UIImage(named: "ic_lock_48pt")

    imageView.image = image

    holderView.backgroundColor = UIColor.lightGray

    holderView.addSubview(imageView)

    imageView.center = CGPoint(x: holderView.frame.size.width / 2, y: holderView.frame.size.height / 2)

    txtEmail.leftView = holderView

( If im doing long process to fix this please let me know ) 如果我正在花很长时间解决此问题,请告诉我

so I wanted to add constraints programatically to fix and below is the code 所以我想以编程方式添加约束以进行修复,下面是代码

    holderView.translatesAutoresizingMaskIntoConstraints = false

    let leadingConstraint = NSLayoutConstraint(item: holderView, attribute: .leading, relatedBy: .equal, toItem: emailView, attribute: .leading, multiplier: 1, constant: 0)

    let trailingConstraint = NSLayoutConstraint(item: holderView, attribute: .trailing, relatedBy: .equal, toItem: emailView, attribute: .trailing, multiplier: 0.12, constant: 0)

    let topConstraint = NSLayoutConstraint(item: holderView, attribute: .top, relatedBy: .equal, toItem: emailView, attribute: .top, multiplier: 1, constant: 0)

    let bottomConstraint = NSLayoutConstraint(item: holderView, attribute: .bottom, relatedBy: .equal, toItem: emailView, attribute: .bottom, multiplier: 1, constant: 0)

//I have txtEmail in another view (emailView) //Error while running any of two lines of code below //我在另一个视图中有txtEmail(emailView)//运行下面两行代码中的任何一条时出错

    emailView.addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint]) //or
   txtEmail.addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])

Kindly suugest me how to fix this 请给我建议如何解决这个问题

Try his code: 试试他的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    textfield.leftViewMode = UITextFieldViewMode.always
    let imageView = UIImageView(frame: CGRect(x: -1, y: 0, width: 30, height: 40))
    let image = UIImage(named: "q.png")
    imageView.backgroundColor = UIColor(white: 0.7, alpha: 0.6)
    imageView.image = image
    textfield.leftView = imageView

    textfield.layer.borderColor = UIColor(white: 0.7, alpha: 0.6).cgColor
    textfield.layer.borderWidth = 2

    imageView.layer.borderColor = UIColor(white: 0.7, alpha: 0.6).cgColor
    imageView.layer.borderWidth = 2

    textfield.layer.cornerRadius = 5
    textfield.layer.masksToBounds = true

 }

Note: You have to create an icon image similar to below image to achieve the effect.Its all about creating an icon and place it in a right position.hope this helps.. 注意:您必须创建一个类似于下图的图标图像才能达到效果,这全部与创建图标并将其放置在正确的位置有关。希望这会有所帮助。

在此处输入图片说明

Output: 输出:

在此处输入图片说明

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

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