图像图标未显示在swift文本框的右侧

时间:2018-03-27 07:27:22

标签: ios swift

  1. 图片位于文本框的右侧,但图片填充不正确:

    let imageView = UIImageView();
    let imageDown = UIImage(named: "down-arrow.png");
    imageView.image = imageDown;
    imageView.frame = CGRect(x: 0, y: 0, width: iconWidth, height: iconHeight)
    pickerSelect.rightViewMode = .always
     pickerSelect.rightView = imageView
    
    1. 他们在文本框中没有填充权限。 如何设置图像填充。

3 个答案:

答案 0 :(得分:0)

保持简单:

txtField.rightViewMode = .always
txtField.rightView = UIImageView(image: UIImage(named: "down-arrow.png"))

答案 1 :(得分:0)

您可以使用以下代码制作自定义文本字段

@IBDesignable
class RDTextField: UITextField {

    @IBInspectable var leftImage:UIImage?{
        didSet{
            if let image = leftImage{
                self.leftViewMode = .always
                let image = UIImageView.init(image: image)
                image.contentMode = .scaleAspectFit
                self.leftView = image
                self.layoutIfNeeded()
            }
        }
    }

    @IBInspectable var RightImage:UIImage?{
        didSet{
            if let image = RightImage{
                self.rightViewMode = .always
                let image = UIImageView.init(image: image)
                image.contentMode = .scaleAspectFit
                self.rightView = image
                self.layoutIfNeeded()
            }
        }
    }

    @IBInspectable var padding = 10

    @IBInspectable var txtpadding = 0


    override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
        var rect = super.leftViewRect(forBounds: bounds)
        rect.origin.x = rect.origin.x + CGFloat(padding)
        return rect
    }

    override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
        var rect = super.rightViewRect(forBounds: bounds)
        rect.origin.x = rect.origin.x - CGFloat(padding)
        return rect
    }

    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        var rect = super.placeholderRect(forBounds: bounds)
        rect.origin.x = rect.origin.x + CGFloat(txtpadding)

        return rect
    }

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        var rect = super.textRect(forBounds: bounds)
        rect.origin.x = rect.origin.x + CGFloat(txtpadding)
        return rect
    }
    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        var rect = super.textRect(forBounds: bounds)
        rect.origin.x = rect.origin.x + CGFloat(txtpadding)
        return rect
    }
}

答案 2 :(得分:0)

使用下面的代码

var imageView = UIImageView()
// Set Image on text Field
    func imageOnTextField(textField : UITextField, imageName : String)
    {
        textField.rightViewMode = UITextFieldViewMode.always
        imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: iconWidth, height: iconHeight))
        let image = UIImage(named: imageName)
        imageView.image = image
        textField.rightView = imageView
        imageView.image = imageView.image!.withRenderingMode(.alwaysTemplate)

    }