Input

To bring something to the back:
In the left hand side of the storyboard, select the item and drag it above the other items.

Textfield
1. Go to the main.storyboard and look for the object library.
2. Look for the textfield object and add it to the screen.
3. Go to the attributes inspector.
4. You can key in placeholder text (text for instructions inside the textfield itself).
5. You specify a type of input, scroll down and adjust keyboard type. (You still need to put some checks in to make sure that it is a number).
6. You can assign an outlet or an action to it.
7. Make correction and spelling checking 'no' in attributes inspector to avoid runtime errors.

Textview
If you want to add multiple lines, you can add a textview instead
Notes about textview:
You should change the background colour to make it visible.
In ViewDidLoad, add:
        self.automaticallyAdjustsScrollViewInsets = false

To make the textview appear above the keyboard at all times:
1. inherit class UITextViewDelegate
2. Add a scrollView to the storyboard and bring it to the back. Create an outlet.
3. Click on constraints at the left hand side. Find the bottom constraint and add an outlet to it.

Then:
@IBOutlet weak var KeywordsToAdd: UITextView!
 
    @IBOutlet weak var scrollView: UIScrollView!
 @IBOutlet var bottomTextfieldConstrain: NSLayoutConstraint!

In ViewDidLoad:
self.KeywordsToAdd.delegate = self
      self.bottomTextfieldConstrain.constant = 50

Implement functions
 func textViewDidBeginEditing(_ textView: UITextView) {
        self.bottomTextfieldConstrain.constant = 230
        let point = CGPoint(x: 0, y: 230)
        scrollView.contentOffset = point
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        self.bottomTextfieldConstrain.constant = 50
    }
    


Checking to see if something has been keyed in:

1. Check to see if you can even get the textfield text property
if let result = fieldName.text
{
 //check to see if something has been keyed in
if(result == "")
{
//like break
 return
}
else
{
}
}

Checking to see if input is a number
if let num = Double(textfieldname.text)
{

}

You can make sure that it is enabled:
textFieldName.isEnabled = true

Make keyboard go away when you tap on screen

For textview:
attribute inspector -> Scrollview -> Keyboard, select dismiss interactively.


OR

1. Create a new swift file.
2. import UIKit
Add:
extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false          
        view.addGestureRecognizer(tap)
    }

    func dismissKeyboard() {
        view.endEditing(true)
    }
}

In every ViewController ViewDidLoad, add:
self.hideKeyboardWhenTappedAround()

Comments

Popular posts from this blog

Setting up a playground

Go to another page