Advertisement
rehannali

Custom textfield with left and right view

Aug 15th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.46 KB | None | 0 0
  1. import UIKit
  2.  
  3. enum ButtonType: Int {
  4.     case leftView
  5.     case rightView
  6. }
  7.  
  8. typealias CompletionButtonPressed = (_ success: Bool, _ type: ButtonType) -> Void
  9.  
  10. class CustomTextField: UITextField {
  11.    
  12.     var completion: CompletionButtonPressed?
  13.    
  14.     lazy var leftViewButton: UIButton = {
  15.         let button = UIButton(type: .system)
  16. //      button.setImage(, for: .normal) Use your image to assign in this line
  17.         button.addTarget(self, action: #selector(handleLeftViewButton), for: .touchUpInside)
  18.         button.frame = CGRect(x: 0, y: 0, width: 20, height: self.frame.size.height) // Use this attribute to give frame for left view
  19. //      button.isUserInteractionEnabled = false Use this line for user interaction If you want, user interact with this button.
  20. //      button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)  Use Inset as per your requirement for image
  21.         return button
  22.     }()
  23.    
  24.     lazy var rightViewButton: UIButton = {
  25.         let button = UIButton(type: .system)
  26. //      button.setImage(, for: .normal)    Use your image to assign in this line
  27.         button.addTarget(self, action: #selector(handleRightViewButton), for: .touchUpInside)
  28.         button.frame = CGRect(x: 0, y: 0, width: 20, height: self.frame.size.height) // Use this attribute to give frame for right view
  29. //      button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)  Use Inset as per your requirement for image
  30. //      button.isUserInteractionEnabled = false Use this line for user interaction If you want, user interact with this button.
  31.         return button
  32.     }()
  33.    
  34.     // MARK: - Use when assign to storyboard -
  35.     override func awakeFromNib() {
  36.         super.awakeFromNib()
  37.         setupInitializationProperties()
  38.     }
  39.    
  40.     // MARK: - Use when programatically initialize -
  41.     override init(frame: CGRect) {
  42.         super.init(frame: frame)
  43.         setupInitializationProperties()
  44.     }
  45.    
  46.     required init?(coder aDecoder: NSCoder) {
  47.         fatalError("init(coder:) has not been implemented")
  48.     }
  49.    
  50.     fileprivate func setupInitializationProperties() {
  51.         leftView = leftViewButton
  52.         rightView = rightViewButton
  53.         rightViewMode = .always
  54.         leftViewMode = .always
  55.     }
  56.    
  57.     // MARK: - Left Button handler
  58.     @objc fileprivate func handleLeftViewButton() {
  59. //      if you want your custom logic, perform here before completion
  60.         completion?(true, .leftView)
  61.     }
  62.    
  63.     // MARK: - Right button handler
  64.     @objc fileprivate func handleRightViewButton() {
  65. //      if you want your custom logic, perform here before completion
  66.         completion?(true, .rightView)
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement