How to move keyboard up over many textFields inside UiView - ios

I have the following in the ViewController:
a) a long viewScroller ( 1000 Height)
b) This long viewScroller contain a UIView Which is 1000 Height, called it 1stView
c) In this 1st UIView (1000 Height), it contain another UiView ( called it 2ndView)
d) in this 2ndUIView( the size: W300x H290) , it contains 5 textFields.
The problems:
1) How to I move the 5 textFields above the Keyboard when user click any one of the textFields to enter data?
for problem (v1) as below
2) below code for problem (V2).
When click outside ( 2ndView or in 2ndView ) the keyboard wont go away.
3) current codition without any implementation for moving the Keyboard up, the 1st TextField can be seen above the keyboard when user click the textField and need to scroll to see the others. Will AppStore approve this User experience?
class ItemViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textField1: UITextField!
#IBOutlet weak var textField2: UITextField!
#IBOutlet weak var textField3: UITextField!
#IBOutlet weak var textField4: UITextField!
#IBOutlet weak var textField5: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField1.delegate = self
textField2.delegate = self
textField3.delegate = self
textField4.delegate = self
textField5.delegate = self
}
//-(V1)- handle textfield
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidBeginEditing(textField: UItextField) {
}
func textFieldDidEndEditing(textField: UITextField) {
}
//-(V2) Called when the user click on the view (outside the UITextField).
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
}
Thanks

Related

Best way to dismiss keyboard when tapping outside of UITextField - IOS

I've found a few threads here about this, and some videos online about it as well, but every solution seems to have problems reported by others. The simplest solution I've found is the one below.
import UIKit
class SignupController: UIViewController, UITextFieldDelegate {
// Outlets
#IBOutlet weak var logoImage: UIImageView!
#IBOutlet weak var nameTF: CustomTextField!
#IBOutlet weak var emailTF: CustomTextField!
#IBOutlet weak var passwordTF: CustomTextField!
#IBOutlet weak var confirmPassTF: CustomTextField!
// Actions
#IBAction func signupButton(_ sender: UIButton) {
}
override func viewDidLoad() {
super.viewDidLoad()
logoImage.image = UIImage(named: "logo2")
nameTF.delegate = self
emailTF.delegate = self
passwordTF.delegate = self
confirmPassTF.delegate = self
}
// Moves to next text field each time return key is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == nameTF {
textField.resignFirstResponder()
emailTF.becomeFirstResponder()
} else if textField == emailTF {
textField.resignFirstResponder()
passwordTF.becomeFirstResponder()
} else if textField == passwordTF {
textField.resignFirstResponder()
confirmPassTF.becomeFirstResponder()
}else if textField == confirmPassTF {
textField.resignFirstResponder()
}
return true
}
// Dismisses keyboard when tapped
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
}
It works, is very simple, but my project and coding experience are in their infancy, so I'm not sure if this is the best method simply because it's short, or if there's something I'm missing due to lack of experience/knowledge?
Anybody know of a better solution, or is this one just fine?
just do this:
class viewController: UIViewController, UITextFieldDelegate {
// Outlets
#IBOutlet weak var logoImage: UIImageView!
#IBOutlet weak var nameTF: CustomTextField!
#IBOutlet weak var emailTF: CustomTextField!
#IBOutlet weak var passwordTF: CustomTextField!
#IBOutlet weak var confirmPassTF: CustomTextField!
// Actions
#IBAction func signupButton(_ sender: UIButton) {
}
override func viewDidLoad() {
super.viewDidLoad()
logoImage.image = UIImage(named: "logo2")
nameTF.delegate = self
emailTF.delegate = self
passwordTF.delegate = self
confirmPassTF.delegate = self
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dissMissKeyboard))
view.addGestureRecognizer(tap)
}
func dissMissKeyboard() {
view.endEditing(true)
}
I prefer to use UITextField delegate method:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
view.endEditing(true)
return true
}
or setup inputAccessoryView which have 'done' or 'exit' button.
Then you need to implement the gesture recognition for this . Or you can do like this :
override func viewDidLoad() {
super.viewDidLoad()
//Looks for single or multiple taps.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dissMissKeyboard))
//Uncomment the line below if you want the tap not not interfere and cancel other interactions.
//tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
//Calls this function when the tap is recognized.
func dissMissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}

Printing UITextField input to console

I am trying to print my input from textfields I have setup to the console before I print them to a label. I've tried string interpolation and various other methods, but I can't seem to get it to print the inputs themselves. I know force unwrapping is frowned upon but I will handle it properly after I get this to work. I've checked other asks extensively but found nothing specific to this in particular
To be clearer, when I press submit I want the text from the textfields to print to the console. Right now what happens is the values only show up at (UITextfield)
class SPProfileViewController: UIViewController {
#IBOutlet weak var typeOfCompany: UITextField!
#IBOutlet weak var firstName: UITextField!
#IBOutlet weak var lastName: UITextField!
#IBOutlet weak var email: UITextField!
#IBOutlet weak var cellPhone: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
typeOfCompany.delegate = self
firstName.delegate = self
lastName.delegate = self
email.delegate = self
cellPhone.delegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
cellPhone.resignFirstResponder()
}
#IBAction func onSubmit(_ sender: UITextField) {
print(self.typeOfCompany.text!)
print(self.firstName.text!)
print(self.lastName.text!)
print(self.email.text!)
print(self.cellPhone.text!)
}
}
extension SPProfileViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
You are watching variables view, Click on "show the console" icon at bottom right corner to view console area.
If that is not your problem than make sure IBAction onSubmit is connected to storyboard.

Swift / Xcode - How to clear a label when touching inside a textfield?

I am creating a simple calculator app - everything works fine but I'd like to clear the result label when the user enters a new number in a textfield (or just touches inside a textfield).
Right now I am using a touchesBegan function to clear the label (which also dismisses the keyboard:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
resultLabel.text = ""
}
Problem is of course that it doesn't matter where the user touches on the screen (for example accidentally doesn't hit the calc button), the label will always be cleared.
Is there a way to clear the "resultLabel" when the user touches the "numberField" textfield?
Thanks!
#IBOutlet weak var resultLabel: UILabel!
#IBOutlet weak var numberField: UITextField!
override func viewDidLoad(){
super.viewDidLoad()
numberField.delegate = self
}
extension viewcontroller: UITextFieldDelegate {
func textFieldDidBeginEditing(textField: UITextField) {
resultLabel.text = ""
}
}
For clear resultlable first thing you need to give delegate to your text filed
numberField.delegate = self
Setup your viewcontroller to implement UITextFieldDelegate
Add below method to your viewcontroller
class viewcontroller: UIViewController,UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
resultLabel.text = ""
}
}
You can use text field Delegate methods on your textField where the user is entering the numbers.
textField.delegate = self
textField.tag = 0
Add a tag to your textField and check it in didBeginEditing.
func textFieldDidBeginEditing(textField: UITextField) {
if textField.tag == 0 //Or whatever tag you attach{
//Do the required task
}
}

iOS: How to get the current visible keyboard type?

How do I find out if the keyboard is of type numeric, Twitter, email, etc...?
edit: Is there a way to detect keyboard type without using an outlet?
Consider that you have tow textFields in the ViewController, You will need to implement textFieldShouldBeginEditing method from UITextFieldDelegate protocol, as follows:
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tfEmail: UITextField!
#IBOutlet weak var tfPassword: UITextField!
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.keyboardType == .emailAddress {
// this is the tfEmail!
}
if textField.isSecureTextEntry {
// this is tfPassword!
}
}
}
Make sure their delegates are connected to the ViewController, programmatically:
tfEmail.delegate = self
tfPassword.delegate = self
or from the Interface Builder.
Note that you can recognize the keyboard type for the current textField by checking its keyboardType property, which is an instance of UIKeyboardType enum:
The type of keyboard to display for a given text-based view. Used with
the keyboardType property.
What about UITextView?
The same exact functionality should be applied when working with UITextViews, but you need to implement textViewDidBeginEditing(_:) method from UITextViewDelegate protocol instead of implementing textFieldShouldBeginEditing. Again, make sure the delegate of the textView is connected to the ViewController.
Also,
If your main purpose of checking the keyboard type is just for recognizing what is the current responded textField/textView, I suggest to do a direct check:
class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
#IBOutlet weak var tfEmail: UITextField!
#IBOutlet weak var tfPassword: UITextField!
#IBOutlet weak var textViewDescription: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
tfEmail.delegate = self
tfPassword.delegate = self
textViewDescription.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField === tfEmail {
// this is the tfEmail!
}
if textField === tfPassword {
// this is tfPassword!
}
}
func textViewDidBeginEditing(_ textView: UITextView) {
if textView === textViewDescription {
// this is description textview
}
}
}
For more information about === operator you might want to check this question/answers.
Hope this helped.
In addition to Ahmad F 's great answer, this is my approach of getting the current keyboard type, at any time:
Step 1: Delegate UITextField
class File: UIViewController, UITextFieldDelegate{//...}
Update viewDidLoad() to this:
#IBOutlet weak var normalTextField: UITextField!
#IBOutlet weak var numberTextField: UITextField!
#IBOutlet weak var emailTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
numberTextField.keyboardType = .numberPad
normalTextField.keyboardType = .default
emailTextField.keyboardType = .emailAddress
numberTextField.delegate = self
normalTextField.delegate = self
emailTextField.delegate = self
}
Step 2: Working with UITextField's methods:
Add a variable called keyboardType, as below:
var keyboardType: UIKeyboardType? = nil
Then, change it whenever a new textField begins editing:
func textFieldDidBeginEditing(_ textField: UITextField) {
keyboardType = textField.keyboardType
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
keyboardType = nil
return true
}
Step 3: Create and call a function like below:
func getCurrentKeyboard() -> String{
if keyboardType == nil{
return "no current keyboard"
}
else if keyboardType == .numberPad{
return "number"
}
else if keyboardType == .emailAddress{
return "email"
}
else{
return "default"
}
}
#IBAction func displayCurrentKeyboard(_ sender: UIButton) {
print(self.getCurrentKeyboard())
}
And this outputs: email / number / no current keyboard / default, depending on the case.
If you want to check which type of keyboard it is with if-else statements, you can change your displayCurrentKeyboard() method to this:
#IBAction func displayCurrentKeyboard(_ sender: UIButton) {
let keyboardString = self.getCurrentKeyboard()
if keyboardString == "number"{
//...
}
else if keyboardString == "email"{
//...
}
else{
//...
}
}
And that's it! You can call this wherever you want in your code with this usage:
let keyboardString = self.getCurrentKeyboard()
NOTE: This method also handles the case of no keyboard visible on the screen, returning no current keyboard, in this case.
Let me know if this helps!

How to move UITextField above keyboard to show text while typing in a UITextField inside a toolBar

I want to replicate the effect of iMessage structure, tapping on a UITextView in the toolBar, keyboard shows up. But I don't know how to display the text while I'm typing. I think I should move the UITextField up when keyboard appears
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var myLabel: UILabel!
#IBOutlet weak var myTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
myTextField.delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
print("return pressed")
self.myLabel.text = myTextField.text
myTextField.resignFirstResponder()
return true
}
}
Try this:
https://github.com/RobinChao/ChatMessageTableViewController
This is perfect for your intention.

Resources