I m trying to implement following functionality in my iOS project. Here is the screen,
If user do not enter anything or entered wrong data in email and mobile textFields, I want to show like this.
Case 1: If user didn't entered the email or entered incorrect email then it will show an message/view with "Enter valid Email Address"
Case 2: If user didn't entered the mobile number or entered incorrect mobile number then it will show an message/view with "Enter valid Mobile Number"
How to achieve this using swift?
You can use a popoverPresentationController
to get a popover
on any of your textFields
.
1. Create a UIViewController
with a UILabel
inside it.
class PopoverVC: UIViewController {
@IBOutlet weak var textLabel: UILabel!
var text: String?
override func viewDidLoad() {
super.viewDidLoad()
self.textLabel.text = self.text
}
}
2. In you ViewController
create an instance of PopoverVC
whenever you want to present the message
. Present it as popoverPresentationController
and pass the instance of textField
over which you want to present the message
, i.e.
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
@IBOutlet weak var textField: UITextField!
@IBAction func onTapButton(_ sender: UIButton) {
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopoverVC") as! PopoverVC
controller.text = "Enter valid Mobile Number"
controller.modalPresentationStyle = .popover
controller.preferredContentSize = CGSize(width: 300 , height: 70)
let popover = controller.popoverPresentationController
popover?.delegate = self
popover?.permittedArrowDirections = .down
popover?.sourceView = self.textField
popover?.sourceRect = self.textField.bounds
self.present(controller, animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
Output: