Skip to content

Instantly share code, notes, and snippets.

@thomaskioko
Last active January 23, 2020 14:52
Show Gist options
  • Save thomaskioko/5c3d0f80ab2fcac6e939 to your computer and use it in GitHub Desktop.
Save thomaskioko/5c3d0f80ab2fcac6e939 to your computer and use it in GitHub Desktop.
Sample code showing how to add icons to UITextFields
class SignInViewController: UIViewController {
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//set the label font style
instagramLabel.font = UIFont(name: "Pacifico", size: 50)
let iconWidth = 20;
let iconHeight = 20;
//Define the imageView
let imageView = UIImageView();
let imageEmail = UIImage(named: "ic_email.png");
imageView.image = imageEmail;
// set frame on image before adding it to the uitextfield
imageView.frame = CGRect(x: 5, y: 5, width: iconWidth, height: iconHeight)
emailTextField.leftViewMode = UITextFieldViewMode.Always
emailTextField.addSubview(imageView)
let imageViewPassword = UIImageView();
let imagePassword = UIImage(named: "ic_password.png");
// set frame on image before adding it to the uitextfield
imageViewPassword.image = imagePassword;
imageViewPassword.frame = CGRect(x: 5, y: 5, width: iconWidth, height: iconHeight)
passwordTextField.leftViewMode = UITextFieldViewMode.Always
passwordTextField.addSubview(imageViewPassword)
//set Padding
let paddingView = UIView(frame: CGRectMake(0, 0, 25, self.emailTextField.frame.height))
emailTextField.leftView = paddingView
let emailPaddingView = UIView(frame: CGRectMake(0, 0, 25, self.passwordTextField.frame.height))
passwordTextField.leftView = emailPaddingView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment