Skip to content

Instantly share code, notes, and snippets.

@aprofromindia
Created December 19, 2019 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aprofromindia/99cd5f441fb38ea1df2a231794c18306 to your computer and use it in GitHub Desktop.
Save aprofromindia/99cd5f441fb38ea1df2a231794c18306 to your computer and use it in GitHub Desktop.
UITextField with Left and Right Image
//
// ImageTextField.swift
// Created by Apro on 17/12/19.
//
import UIKit
@IBDesignable
class ImageTextField: UITextField {
@IBInspectable var leftIcon: UIImage?
@IBInspectable var rightIcon: UIImage?
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
private func setup() {
padding = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 0)
}
override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
var imgRect = super.leftViewRect(forBounds: bounds)
imgRect.origin.x += 10
return imgRect
}
override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
var rightRect = super.rightViewRect(forBounds: bounds)
rightRect.origin.x -= 100
return rightRect
}
override func draw(_ rect: CGRect) {
if let leftIcon = leftIcon {
leftView = UIImageView(image: leftIcon)
leftViewMode = .always
}
if let rightIcon = rightIcon {
rightView = UIImageView(image: rightIcon)
rightViewMode = .always
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment