Create a stunning textfield with icon in Swift4

Опубликовано: 13 Октябрь 2024
на канале: Divyesh Gondaliya
791
11

Learn to create a stunning textfield with icon in Swift

Collectionview inside ScrollView :-    • Collectionview inside Scrollview  
Dynamic height TableView in a Scroll View :-    • Dynamic height TableView in a Scroll ...  

Step 1: Add a basic UITextField and give it basic constraints

Step 2: Connect the UITextField to its ViewController's IBOutlet:

iconTextField

Step 3: Add Icon to Assets.xcassets and set Render As to Template Image:

Step 4: Extend UITextField and create a new setIcon function:

extension UITextField {
func setIcon(_ image: UIImage) {
let iconView = UIImageView(frame:
CGRect(x: 10, y: 5, width: 20, height: 20))
iconView.image = image
let iconContainerView: UIView = UIView(frame:
CGRect(x: 20, y: 0, width: 30, height: 30))
iconContainerView.addSubview(iconView)
leftView = iconContainerView
leftViewMode = .always
}
}

Step 5: Set the icon and apply tint color!
{
didSet {
iconTextField.tintColor = UIColor.lightGray
iconTextField.setIcon(imageLiteral(resourceName: "icon-user"))
}
}

My Social Media link is below
-------------
instagram :-   / gdivyesh40  
facebook :- https://www.facebook.com/gdivyesh14340#_
Facebook Page :   / iosinfoandknowledge