uitextfield add image icon right

Swift 5

//MARK: - CustomTextView
class BaseTextField: UITextField {
    
    var setHPad: UIEdgeInsets = { return UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8 ) }()

    override open func textRect( forBounds bounds: CGRect ) -> CGRect {
        return bounds.inset( by: self.setHPad )
    }

    override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: self.setHPad )
    }

    override open func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: self.setHPad )
    }

    @IBInspectable open var leftImage:String? {
        didSet {
            if (leftImage != nil) {
                self.applyLeftImage(leftImage!)
                self.setHPad = { return UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 30 ) }()
            }
        }
    }


    fileprivate func applyLeftImage(_ image: String) {

        
        // set image
        let imageView = UIImageView()
        imageView.image = UIImage( systemName: image )
        imageView.tintColor = UIColor(named: K.EpColors.black)
        imageView.clipsToBounds = true
        
        // set holder
        let viewHolder = UIView()
        viewHolder.clipsToBounds = true
        viewHolder.addSubview(imageView)

        // prepare for constraints
        imageView.translatesAutoresizingMaskIntoConstraints = false
        viewHolder.translatesAutoresizingMaskIntoConstraints = false
        
        // views param
        let viewsDictionary = [
            "imageHolder": viewHolder,
            "image":imageView
        ]
        
        let metrics = [
            "default": 8,
            "none": 0
        ]
        
        // set horisontal constraints
        viewHolder.addConstraints(
            NSLayoutConstraint.constraints(withVisualFormat: "H:|-(default)-[image]-(default)-|",
                                           options: [],
                                           metrics: metrics,
                                           views: viewsDictionary))
        
        // set vertical constraints
        viewHolder.addConstraints(
            NSLayoutConstraint.constraints(withVisualFormat: "V:|-(default)-[image]-(default)-|",
                                           options: [],
                                           metrics: metrics,
                                           views: viewsDictionary))

        // add image to textfield
        self.addSubview(viewHolder)
        
        // keep childView from overflowing
        self.clipsToBounds = true
        
        // set horizontal constraints
        self.addConstraints(
            NSLayoutConstraint.constraints(withVisualFormat: "H:|-(>=default)-[imageHolder]-(none)-|",
                                           options: [],
                                           metrics: metrics,
                                           views: viewsDictionary)
        )
        // set vertical constraints
        self.addConstraints(
            NSLayoutConstraint.constraints(withVisualFormat: "V:|-(none)-[imageHolder]-(none)-|",
                                           options: [],
                                           metrics: metrics,
                                           views: viewsDictionary)
        )
        
        self.rightView?.tintColor = UIColor( named: K.EpColors.black )
        
        
    }
}



Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source