Swift UITextField图标位置设置

时间:2015-09-12 10:41:54

标签: swift uitextfield

使用

在UITextField中添加图标
var leftImageView = UIImageView()
leftImageView.image = leftImage
textField.leftView = leftImageView
textField.leftViewMode = UITextFieldViewMode.Always
leftImageView.frame = CGRectMake(15, 10, 15, 20)
textField.addSubview(leftImageView)

Image is close to textfield border

的o / p

我找到了解决方案,例如从上面的代码中删除此代码

textField.leftView = leftImageView

它可以正确地进行图标对齐,但只要开始编辑图标上的文本字段文本,就像这样enter image description here

我想要这样的o / p

enter image description here

4 个答案:

答案 0 :(得分:12)

首先,您绝对不应将图像作为子视图添加到标签中。它足以设置leftView属性。

textField.addSubview(leftImageView) // Delete this line

其次,忽略应用于左视图框架的任何x或y偏移。文本字段仅关注视图的大小。如果要在图像周围添加填充,一个选项是使用容器视图并将图像视图放在其中。

let leftImageView = UIImageView()
leftImageView.image = leftImage

let leftView = UIView()
leftView.addSubview(leftImageView)

leftView.frame = CGRectMake(0, 0, 30, 20)
leftImageView.frame = CGRectMake(0, 0, 15, 20)

textField.leftView = leftView

另一种选择是继承UITextField并覆盖leftViewRectForBounds

答案 1 :(得分:4)

引用@hennes语法,swift中缺少一些语法,例如CGRect语法已更改且缺少userNameTextField.leftViewMode = .always

使用swift语法,这对我有用:

    let leftImageView = UIImageView()
    leftImageView.image = UIImage(named: "email")

    let leftView = UIView()
    leftView.addSubview(leftImageView)

    leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
    leftImageView.frame = CGRect(x: 10, y: 10, width: 20, height: 20)
    userNameTextField.leftViewMode = .always
    userNameTextField.leftView = leftView

答案 2 :(得分:2)

试试这个。可以帮到你。

var padding: Float = 20

var envelopeView: UIImageView = UIImageView(frame: CGRectMake(padding, 0, 30, 30))
envelopeView.image = UIImage.imageNamed("comment-128.png")
envelopeView.contentMode = UIViewContentModeScaleAspectFit

var viewLeft: UIView = UIView(frame: CGRectMake(padding, 0, 30, 30))
viewLeft.addSubview(envelopeView)
textField.leftView.setFrame(envelopeView.frame)
textField.leftView = viewLeft
textField.leftViewMode = UITextFieldViewModeAlways

var viewRight: UIView = UIView(frame: CGRectMake(textField.frame.size.width - (textField.frame.size.width + 30 + padding), 0, 30, 30))
viewRight.addSubview(envelopeView)
textField.rightView.setFrame(envelopeView.frame)
textField.rightView = viewRight
textField.rightViewMode = UITextFieldViewModeAlways

答案 3 :(得分:1)

Swift 3.1

extension UITextField
{
    enum Direction
    {
        case Left
        case Right
    }

    func AddImage(direction:Direction,imageName:String,Frame:CGRect,backgroundColor:UIColor)
    {
        let View = UIView(frame: Frame)
        View.backgroundColor = backgroundColor

        let imageView = UIImageView(frame: Frame)
        imageView.contentMode = .center
        imageView.image = UIImage(named: imageName)

        View.addSubview(imageView)

        if Direction.Left == direction
        {
            self.leftViewMode = .always
            self.leftView = View
        }
        else
        {
            self.rightViewMode = .always
            self.rightView = View
        }
    }
}