Hello guys, Today we learn about How to Show Hint View In Swift.
Sometimes, we need to add a hint view for user preference where the user can get the exact meaning of your point. for example, if I am using a text field like password and I want to give any instruction regarding the password field for the user then I can use hint view. The hint view is like a pop-up view.
Getting Started:-
Step1:- Firstly, Create an XCode project.
File–> New–> Project–> iOS–> Next And add your project name then create.
Step 2:- Secondly, Create a UI design for the view in the stoaryboard.
Step 4:- For hint view, we need to install cocoa pods into our project
1 |
pod "AMPopTip" |
How to install the pod file-
- open terminal
- write pod init
- Then open profile
- add pod “pod “AMPopTip”
- And, run the pod install command
- open .xcworkspace project
To know more about this pod please click here.
Step 5:- To call the pop-Tip function we will create a class and add the code
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import UIKit import AMPopTip struct popupInfoMessageView{ static let popTipView = PopTip() static func show(sender:UIButton,message:String,view:UIView){ popTip.shouldDismissOnTap = true popTip.shouldDismissOnTapOutside = true popTip.shouldDismissOnSwipeOutside = true popTip.bubbleColor = .black popTip.show(text: message, direction: .down, maxWidth: SCREEN_WIDTH-16, in: view, from: sender.frame) } } |
Step 6:- Lastly, Add the code in your view controller class and run the project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import UIKit import AMPopTip let SCREEN_WIDTH = ((UIApplication.shared.statusBarOrientation == .portrait) || (UIApplication.shared.statusBarOrientation == .portraitUpsideDown) ? UIScreen.main.bounds.size.width : UIScreen.main.bounds.size.height) class ViewController: UIViewController { @IBOutlet weak var passwordHint: UIButton! @IBOutlet weak var nameHint: UIButton! @IBOutlet weak var passwordLabel: UITextField! @IBOutlet weak var nameLabel: UITextField! var isNameHint:Bool = false var isPasswordHint:Bool = false override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func nameHintClick(_ sender: UIButton) { isNameHint = !isNameHint if isNameHint{ popupInfoMessageView.popTipView.removeFromSuperview() popupInfoMessageView.show(sender:sender,message:"Enter your FirstName and LastName",view:view) }else{ popupInfoMessageView.popTipView.hide() } } @IBAction func passwordHintClick(_ sender: UIButton) { isPasswordHint = !isPasswordHint if isPasswordHint{ popupInfoMessageView.popTipView.removeFromSuperview() popupInfoMessageView.show(sender:sender,message:"Enter your Password, it should be contain capital and small letter and number's ",view:view) }else{ popupInfoMessageView.popTipView.hide() } } } |
And the output is-
Conclusion:-
In this blog, we discussed How to Show a Hint View In Swift.
I hope this blog will help you to understand the flow of hint view. For hint view, we can create a custom view in the storyboard and add the hide-show functionality accordignly.
To learn about a more iOS-related topic, Please click here
Thanks for reading!!