Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- enum ButtonType: Int {
- case leftView
- case rightView
- }
- typealias CompletionButtonPressed = (_ success: Bool, _ type: ButtonType) -> Void
- class CustomTextField: UITextField {
- var completion: CompletionButtonPressed?
- lazy var leftViewButton: UIButton = {
- let button = UIButton(type: .system)
- // button.setImage(, for: .normal) Use your image to assign in this line
- button.addTarget(self, action: #selector(handleLeftViewButton), for: .touchUpInside)
- button.frame = CGRect(x: 0, y: 0, width: 20, height: self.frame.size.height) // Use this attribute to give frame for left view
- // button.isUserInteractionEnabled = false Use this line for user interaction If you want, user interact with this button.
- // button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) Use Inset as per your requirement for image
- return button
- }()
- lazy var rightViewButton: UIButton = {
- let button = UIButton(type: .system)
- // button.setImage(, for: .normal) Use your image to assign in this line
- button.addTarget(self, action: #selector(handleRightViewButton), for: .touchUpInside)
- button.frame = CGRect(x: 0, y: 0, width: 20, height: self.frame.size.height) // Use this attribute to give frame for right view
- // button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) Use Inset as per your requirement for image
- // button.isUserInteractionEnabled = false Use this line for user interaction If you want, user interact with this button.
- return button
- }()
- // MARK: - Use when assign to storyboard -
- override func awakeFromNib() {
- super.awakeFromNib()
- setupInitializationProperties()
- }
- // MARK: - Use when programatically initialize -
- override init(frame: CGRect) {
- super.init(frame: frame)
- setupInitializationProperties()
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- fileprivate func setupInitializationProperties() {
- leftView = leftViewButton
- rightView = rightViewButton
- rightViewMode = .always
- leftViewMode = .always
- }
- // MARK: - Left Button handler
- @objc fileprivate func handleLeftViewButton() {
- // if you want your custom logic, perform here before completion
- completion?(true, .leftView)
- }
- // MARK: - Right button handler
- @objc fileprivate func handleRightViewButton() {
- // if you want your custom logic, perform here before completion
- completion?(true, .rightView)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement