Created
December 19, 2019 08:51
-
-
Save aprofromindia/99cd5f441fb38ea1df2a231794c18306 to your computer and use it in GitHub Desktop.
UITextField with Left and Right Image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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