How to resize ScrollView when keyboard appears? - ios

I'm trying to create this post feature which would allow you to insert text as well as images (similar to Note app on iOS devices). However, I can't figure out how to move or resize my ScrollView when the keyboard appears when I press on the light-grey area (it's a text view field), so the grey area would resize and the add button would move above the keyboard when it appears.
// PostViewController.swift
//
// Created by Martynas on 09/12/2016.
// Copyright © 2016 Martynas. All rights reserved.
//
import UIKit
import Firebase
class PostViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var ScrollView: UIScrollView!
#IBOutlet var titleTextField: UITextField!
#IBOutlet var contentTextField: UITextView!
#IBOutlet var Menu: UIView!
#IBAction func hideKeyboardWhenSwippedDown(_ sender: Any) {
contentTextField.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Hide keyboard when...
self.hideKeyboardWhenTappedAround() // ...press anywhere outside the keyboard
self.titleTextField.delegate = self
self.contentTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func sendTapped(_ sender: Any) {
if let uid = FIRAuth.auth()?.currentUser?.uid {
if let title = titleTextField.text {
if let content = contentTextField.text {
let postObject: Dictionary<String, Any> = [
"uid": uid,
"title": title,
"content": content
]
FIRDatabase.database().reference().child("posts").childByAutoId().setValue(postObject)
}
}
}
}
#IBAction func addTapped(_ sender: Any) {
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == contentTextField {
ScrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
} else {
return
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField == contentTextField {
ScrollView.setContentOffset(CGPoint(x: 0, y: 250), animated: true)
} else {
return
}
}
// Hide keyboard when user presses 'return' key on the keyboard...
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
titleTextField.resignFirstResponder()
return true
}
}
This is the Controller View:

You need to listen to the keyboardDidShow/Hide notification and adjust the scroll view's height accordingly. The keyboardDidShow notification userInfo contains the keyboard's frame and therefore height.
Presuming your scroll view has a constraint at the bottom of the super view, you can make it an IBOutlet and animate it's constant to the height of the keyboard and back to 0 when the keyboard show and hide notifications are fired respectively.

Related

Disable / Enable a button in Xcode

Hi i'm new with Swift programming.
What im trying to do is Disable my button (signIn) in viewDidLoad and only enable when the textfields have text in them. Here's what i've achieved so far. (not much though!)
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
signIn.isEnabled = false
// Do any additional setup after loading the view, typically from a nib.
}
#IBOutlet weak var emailtxt: UITextField!
#IBOutlet weak var passwordtxt: UITextField!
#IBOutlet weak var signIn: UIButton!
I need help to create a function in signIn that keeps button disabled until text fields (emailtxt & passwordtxt) have text in them and then proceed.
Glad if anyone can sort me.
Thanks in advance!
First add these for all of your textFields in viewDidLoad():
emailtxt.addTarget(self, action: #selector(textFieldDidChange(_:)),
for: UIControlEvents.editingChanged)
passwordtxt.addTarget(self, action: #selector(textFieldDidChange(_:)),
for: UIControlEvents.editingChanged)
Then use this:
#objc func textFieldDidChange(_ textField: UITextField) {
self.buttonIsEnabled()
}
func buttonIsEnabled() {
var buttonIsEnabled = true
defer {
self.signIn.isEnabled = buttonIsEnabled
}
guard let emailtxt = self.emailtxt.text, !emailtxt.isEmpty else {
addButtonIsEnabled = false
return
}
guard let passwordtxt = self. passwordtxt.text, ! passwordtxt.isEmpty else {
addButtonIsEnabled = false
return
}
}
I use this way in my codes and it works well.
Even you can add more methods for additional checking to buttonIsEnabled, like:
self.checkEmailIsValid(for: emailtxt)
Of course you should handle this method before:
func checkEmailIsValid(for: String) {
//...
}
Set ViewController as delegate for emailtxt and passwordtxt like this,
override func viewDidLoad() {
super.viewDidLoad()
signIn.isEnabled = false
emailtxt.delegate = self
passwordtxt.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
Conform your ViewController to UITextFieldDelegate and enable/disable as the text input is finished,
extension ViewController: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
if emailtxt.text?.isEmpty == false && passwordtxt.text?.isEmpty == false {
signIn.isEnabled = true
} else {
signIn.isEnabled = false
}
}
}
Here is the fix for your code you shared.
import UIKit
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
#objc func dismissKeyboard() {
view.endEditing(true)
}
}
extension SignInVC: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
if emailtxt.text?.isEmpty == false && passwordtxt.text?.isEmpty == false {
signIn.isEnabled = true
} else {
signIn.isEnabled = false
}
}
}
class SignInVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
signIn.isEnabled = false
emailtxt.delegate = self
passwordtxt.delegate = self
self.hideKeyboardWhenTappedAround()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var emailtxt: UITextField!
#IBOutlet weak var passwordtxt: UITextField!
#IBOutlet weak var signIn: UIButton!
}
What I would do is create an IBAction from one of your text fields, and set the event to Editing Changed:
The code should look like this:
#IBAction func textFieldEditingDidChange(_ sender: UITextField) {
}
You can then connect that same outlet to both of your text fields by dragging from the outlet to your remaining field. If you've connected both correctly, clicking on the circle to the left of your IBAction should show two text fields:
The action will now be fired every time text changes in either of your fields.
Then, at the top of the file, I'd create a computed property that returns false unless there is something in both fields:
var shouldEnableButton: Bool {
guard let text1 = textField1.text, let text2 = textField2.text else {
return false
}
return text1.isEmpty && text2.isEmpty ? false : true
}
Finally, we add shouldEnableButton to our IBAction:
#IBAction func textFieldEditingDidChange(_ sender: UITextField) {
button.isEnabled = shouldEnableButton
}
Important
When you connect your second text field to the outlet, it will incorrectly assign Editing Did End as its event:
Delete this event and click and drag from Editing Changed to your IBAction:
Use SwiftValidator
https://github.com/SwiftValidatorCommunity/SwiftValidator
by this, you will set validation of email & password like below
import SwiftValidator
let validator = Validator()
validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule(message: "Invalid email")])
// MARK: - ValidationDelegate
extension ViewController: ValidationDelegate {
func validationSuccessful() {
self.loginUser()
}
func validationFailed(_ errors:[(Validatable ,ValidationError)]) {
for (field, error) in errors {
//Handle as per need - show extra label - shake view etc
/*
if let field = field as? UITextField {
Utilities.shakeTheView(shakeView: field)
}
error.errorLabel?.text = error.errorMessage
error.errorLabel?.isHidden = false
*/
}
}
}

TableView in a popup Swift

please can someone tell me if i can put a tableView in a popup View. I have been using a function called showStandardDialog from PopupDialog, here some code:
func showStandardDialog(a: String, b:String) {
// Prepare the popup
let title = "¡ INFORMACIÓN !"
let message = "this is " + a + "and this is " + b
// Create the dialog
let popup = PopupDialog(title: title, message: message, buttonAlignment: .horizontal, transitionStyle: .zoomIn, gestureDismissal: true) {
print("Completed")
}
// Create first button
let buttonOne = CancelButton(title: "CANCEL") {
}
// Create second button
let buttonTwo = DefaultButton(title: "OK") {
}
// Add buttons to dialog
popup.addButtons([buttonOne, buttonTwo])
// Present dialog
self.present(popup, animated: true, completion: nil)
}
But i don´t know how i can use this same function and library sending a list.
Thanks to all :)
PopupDialog is a 3rd party lib and if you need something super custom you should consider doing it yourself.
That being said if you want to use this lib, take a look at how the RatingViewController is done in the examples:
//
// RatingViewController.swift
// PopupDialog
//
// Created by Martin Wildfeuer on 11.07.16.
// Copyright © 2016 CocoaPods. All rights reserved.
//
import UIKit
class RatingViewController: UIViewController {
#IBOutlet weak var cosmosStarRating: CosmosView!
#IBOutlet weak var commentTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
commentTextField.delegate = self
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(endEditing)))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#objc func endEditing() {
view.endEditing(true)
}
}
extension RatingViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
endEditing()
return true
}
}
There is no reason why you couldn't add a TableView in the xib and in the ViewController include the necessary delegate and data source.

Not able to set textfield as becomeFirstResponder() with textfield outlet collection in tvos

I took text field outlet collection and bind six text field over there.
I want to becomeFirstResponder of next text field which is in text field outlet collection.
I gave textfields tag 0 to 5 from storyboard.
see,
Main ViewController:
class ViewController: UIViewController {
#IBOutlet var txtSignUp: [UITextField]!
var arrayPlaceHolder:NSArray!
override func viewDidLoad() {
super.viewDidLoad()
arrayPlaceHolder = NSArray(array: ["1","2","3","4","5","6"])
self.setTextFieldValue()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func setTextFieldValue(){
for txtField in txtSignUp{
let tagTxt = txtField.tag
txtField.attributedPlaceholder = NSAttributedString(string:arrayPlaceHolder[tagTxt] as! String, attributes:[NSForegroundColorAttributeName:UIColor.black])
if(tagTxt != ((arrayPlaceHolder.count) - 1)){
txtField.returnKeyType = .next
}
txtField.delegate = self
}
}
}
extension ViewController:UITextFieldDelegate{
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
for txt in txtSignUp{
let nextTxt = (textField.tag + 1)
if txt.tag == nextTxt {
txt.becomeFirstResponder()
break
}
}
return true
}
}
Error:
whose view is not in the window hierarchy!
Explanation:
In this code, I am not able to become next text field as becomeFirstResponder.
Can anyone help me to resolve this issue.
On TVos you have to use the textFieldDidEndEditing function because textFieldShouldReturn won't work to set the next responder:
class MyViewController: UIViewController{
#IBOutlet weak var firstTextField: UITextField!
#IBOutlet weak var secondTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
firstTextField.delegate = self
secondTextField.delegate = self
firstTextField.tag = 0
secondTextField.tag = 1
}
}
extension MyViewController: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
if (textField.tag == 0){
secondTextField.becomeFirstResponder()
}
}
}

How to enable a UIBarItem after checking a TextField is not empty

This is my first question in this forum. I would like to know how to enable a UIBarButtonItem only if a UITextField has text in it. I have seen many examples of how to do this in Swift 2 but not Swift 3. This is for a simple app to save some data to core data. I do not have any code to show other then the UIOutlets etc as I am unsure how to check the UITextField for text on the fly.
You need to check length of textField.
//MARK: - UITextFieldDelegate
func textFieldDidEndEditing(_ textField: UITextField) {
if let text = exampleTextFiled.text {
exampleBarButtonItem.isEnabled = text.characters.count > 0
}
}
At the top of you controller you need to add UITextFieldDelegate
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var exampleTextField: UITextField!
#IBOutlet var exampleBarButtonItem: UIBarButtonItem!
// Your code
}
And in the storyboard you can ctrl-drag from textField to controller to add delegate.
That's why I don't recommend using Interface Builder and Storyboards, the developer gets "dumbed down".
Anyhow, the algorithm is something like this:
1) Text fields have callback methods or "delegate" methods for text field events like user tapping on the return/done key.
So firstly, you need to tell your text field the delegate is your view controller.
2) You define the necessary UITextField delegate methods (https://developer.apple.com/reference/uikit/uitextfielddelegate)
3) There is a hasText method (newly available for iOS 10+) for UITextField to tell if your text field has any text. If there is, enable the bar button item, otherwise disable it.
You could alternatively use if let optional binding to unwrap your textField.text? property, then check the text.characters.count is greater than 0 like so:
if let text = textField.text {
navigationItem.rightBarButtonItem?.isEnabled = text.characters.count > 0
}
This is the conventional way of doing it (before iOS 10 if you want to support older iOS version) :D
The code in full is below:
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var myTextField:UITextField!
...
override func viewDidLoad() {
super.viewDidLoad()
// self is referring to this view controller
...
myTextField.returnKeyType = UIReturnKeyType.done
myTextField.delegate = self
}
// UITextField Delegate Methods
func textFieldDidEndEditing(_ textField: UITextField) {
// get the navigation bar button (right bar button in my case)
// and set its "isEnabled" property depending on whether
// the textField has any text using "hasText" of UITextField
navigationItem.rightBarButtonItem?.isEnabled = textField.hasText
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
hideKeybaord()
return false
}
// UITouches delegate methods for extra user experience polish
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// hides keyboard when user taps on the background
hideKeybaord()
}
// resusable hideKeyboard method
func hideKeybaord() {
myTextField.resignFirstResponder()
// in case you need to hide all other text fields in the future
// textField2.resignFirstResponder()
// textField3.resignFirstResponder()
}
}
Below is the code I implemented to resolve my problem. It does not take into consideration the iOS keyboard. The last function is the #IBAction (Editing Changed) added to the controller (ViewController.swift) from the TextField in the Scene. I right clicked the TextField in the scene selected the Action, Editing Changed and dragged it to the View Controller. Thanks to all for your iuput and code!!
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
// Add Outlet
#IBOutlet weak var tvShowName: UITextField!
#IBOutlet weak var saveButton: UIBarButtonItem!
// Add functions
#IBAction private func saveButton(_ sender: UIBarButtonItem) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let newTVShow = TVShows(context: context)
newTVShow.name = tvShowName.text
do {
try context.save()
navigationController!.popViewController(animated: true)
} catch {
let alert = UIAlertController(title: "Oops!", message: "Failed to save the TV Show", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
#IBAction private func cancelButton(_ sender: UIBarButtonItem) {
// Code for cancel
navigationController!.popViewController(animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
saveButton.isEnabled = false
}
#IBAction func tvShowTextFieldEditChanged(_ sender: UITextField) {
if let text = tvShowName.text{
saveButton.isEnabled = text.characters.count > 0
}
}
}

(Xcode 8 Swift 3). Using Custom Keyboard Extension with a particular Text Field

Since standard Number Pad keyboard has "empty" button, but doesn't have "+/-" button, I decided to create my own Keyboard Extension. I've done it.
But I don't know how to link (and invoke) it with a particular Text Field while other Text Fields using usual keyboards.
Is there any opportunity to apply custom keyboardType like my own custom Keyboard?
I found solution based on simular question: How to input text using the buttons of an in-app custom keyboard
import UIKit
protocol KeyboardDelegate: class {
func keyWasTapped(text: String)
}
class KeyboardView: UIView {
// This variable will be set as the view controller so that
// the keyboard can send messages to the view controller.
weak var delegate: KeyboardDelegate?
// MARK:- keyboard initialization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initializeSubviews()
}
override init(frame: CGRect) {
super.init(frame: frame)
initializeSubviews()
}
func initializeSubviews() {
let xibFileName = "KeyboardView" // xib extention not included
let view = Bundle.main.loadNibNamed(xibFileName, owner: self, options: nil)?[0] as! UIView
self.addSubview(view)
view.frame = self.bounds
}
// MARK:- Button actions from .xib file
#IBAction func keyTapped(_ sender: UIButton) {
// When a button is tapped, send that information to the
// delegate (ie, the view controller)
self.delegate?.keyWasTapped(text: sender.titleLabel!.text!) // could alternatively send a tag value
}
}
/* when error: "Could not load NIB in bundle"
Could not load NIB in bundle
Visit the properties of the .xib files in the file inspector ,the property "Target Membership" pitch on the select box ,then your xib file was linked with your target
*/
In main ViewController.swift
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, KeyboardDelegate {
#IBOutlet weak var text1: UITextField!
#IBOutlet weak var text2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// initialize custom keyboard
let keyboardView = KeyboardView(frame: CGRect(x: 0, y: 0, width: 375, height: 165))
keyboardView.delegate = self // the view controller will be notified by the keyboard whenever a key is tapped
// replace system keyboard with custom keyboard
text1.inputView = keyboardView //accessoryView
text1.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// required method for keyboard delegate protocol
func keyWasTapped(text character: String) {
if Int(character) != nil{
text1.insertText(character)
}
if character == "⌫" {
if !(text1.text?.isEmpty)! {
let beforeText = text1.text!
let truncated = beforeText.substring(to: beforeText.index(before: beforeText.endIndex))
text1.text = truncated
}
}
if character == "±" {
let beforeText = text1.text!
if var number = Int(beforeText) {
number = -number
text1.text = "\(number)"
}
}
}
func textFieldDidBeginEditing(_ textField: UITextField) {
/*
if (textField == self.text1) {
//textField.inputView = keyboardView
}
*/
}
}
I have coded a calculator for metric/imperial system. For that, I have coded my own keyboard, too.
I have set up the keyboard as UIButtons stacked within a Stack View.
The Buttons seem to be unknown for Xcode since I have just upgraded to Xcode 8 and the project is still in Swift 2.2.
Then I have set a UITextField and filled its text property using my buttons. This is for example the function for the Button 1 on my keyboard.
#IBOutlet weak var inputField: UITextField!
var numberStr:String = "0"
inputField.text = numberStr
#IBAction func oneKeyboardAction(sender: AnyObject) {
if numberStr == "0" {
numberStr = String(numberStr.characters.dropLast())
}
let newStr:String = numberStr + String("1")
numberStr = newStr
let dotToCommaString = newStr.stringByReplacingOccurrencesOfString(".", withString: ",")
inputField.text = dotToCommaString
}
Also I have deactivated user interaction with the TextField, so the "original Keyboard" will not show.
Edit
Like mentioned in the comment section and to have my answer better fit your needs. You could set my custom keyboard into a UIView overlapping your UIViewController inside the Interface Builder. Set it as MyKeyboardView.hidden = true inside the viewDidLoad().
Then you have your TextField where you want the custom Keyboard to be visible instead of the system one:
func textFieldDidBeginEditing(_ textField: UITextField) {
if (textField == self.yourDesiredTextField) { //the one where you want to use the custom keyboard
MyKeyboardView.hidden = false
}
}
Then you add a gesture recognizer like that:
override func viewDidLoad() {
super.viewDidLoad()
MyKeyboardView.hidden = true
let tap = UITapGestureRecognizer(target: self, action: #selector(gesture))
tap.numberOfTapsRequired = 1
view.addGestureRecognizer(tap)
}
func gesture() {
UIView.animateWithDuration(0.2) {
self.MyKeyboardView.hidden = true
}
}
To have it little more smooth, I have added animateWithDuration when hiding the keyboard.

Resources