var hideShowbtn = UIButton () hideShowbtn = UIButton (type: . custom ) hideShowbtn . setImage ( UIImage (named: "HIdeShowImg" ), for: . normal ) hideShowbtn . frame = CGRect (x: CGFloat ( self . passwordTF . frame . size . width - 25 ), y: CGFloat ( 0 ), width: CGFloat ( 25 ), height: CGFloat ( 25 )) hideShowbtn . addTarget ( self , action: #selector ( self . refresh ), for: . touchUpInside ) self . passwordTF . rightView = hideShowbtn self . passwordTF . rightViewMode = . always Happy Coding :)