Password TextField for iOS

Add new File in Xcode
Adding new Swift File in Xcode
import UIKitclass PasswordTextField: UITextField {}
override init(frame: CGRect) {    super.init(frame: frame)}
required init?(coder aDecoder: NSCoder) {    super.init(coder: aDecoder)}
private func setup() {
self.isSecureTextEntry = true
}
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
button.setImage(UIImage(named: “ic_visibility_off”), for: .normal)
button.setImage(UIImage(named: “ic_visibility_on”), for: .selected)
button.addTarget(self, action: #selector(showHidePassword(_:)), for: .touchUpInside)@objc private func showHidePassword(_ sender: UIButton) {    sender.isSelected = !sender.isSelected
self.isSecureTextEntry = !sender.isSelected
}
rightView = button
rightViewMode = .always

The full source code:

PasswordTextField.swift
let passwordTextField = PasswordTextField(frame: CGRect(x: 0, y: 0, width: 400, height: 50))
view.addSubview(passwordTextField)

Real Word Usage

Password TextField in Change Password

--

--

--

iOS Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build and Deploy Docker Image to GCP Kubernetes cluster with Jenkins Pipeline

A simple example for Helm chart

Hotwheels, hot coffee and dangerous parenting precedents

Five lessons learned while learning to Code

Code Deployment with Serverless Application in AWS

Testing your Terraform project with Terratest

Architecture Made Easy: 4 Simple Ways you can Perfect your Code Design

Python Dictionary Comprehension

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mukesh Shakya

Mukesh Shakya

iOS Developer

More from Medium

How to securely handle the Third-party Keys in iOS?

Design an APP Routing(updating. . .)

Swift — Network Requests and JSON Parsing 101

My first native iOS app: YOURA