Issue with textview in swift 3 - swift

I am working in a form in swift 3, my problem is when I tried to hide the keyboard in textview because the keyboard reuse to hide and is still visible in the previous VC.
My question is what I missing to fix this and when tap outside of textview the keyboard hide automatically?
Here the code:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return(true)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if(textField == names){
scrooView.setContentOffset(CGPoint(x:0, y:250), animated: true)
}
else if(textField == email){
scrooView.setContentOffset(CGPoint(x:0, y:250), animated: true)
}
else if (textField == tel){
scrooView.setContentOffset(CGPoint(x:0, y:250), animated: true)
}
else if (textField == message){
scrooView.setContentOffset(CGPoint(x:0, y:450), animated: true)
}
}
func textFieldDidEndEditing(_ textField: UITextField, reason:
UITextFieldDidEndEditingReason) {
scrooView.setContentOffset(CGPoint(x:0, y:0), animated: true)
}
extension UIViewController{
func hideKeyboard(){
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
#objc func dismissKeyboard(){
view.endEditing(true)
}
}
Here a link with the issue!

You are using a scrollView, try to add the GestureRecognizer in them, like this:
class ViewController: UIViewController {
#IBOutlet weak var scrooView: UIScrollView!
#IBOutlet weak var tfTest: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
scrooView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)))
// 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.
}
#objc func dismissKeyboard(){
tfTest.endEditing(true)
}
})
If you add your Gesture in your View, it could never actived 'cause you are touching the scrollview that is over the view.

Related

Move view using Scroll View to prevent keyboard from obscuring text field or text view

I am implementing an app where tapping a button will pop up a location entry screen which is implemented with Modally presented Segue and Visual Effect with Blur to make the entry window appear hovering above the main screen.
The pop up screen has a text field for location name and a multi line text view for remarks.
Problem is the multi line text view is partially obscured by the keyboard on smaller devices and in landscape view.
Landscape View
Portrait View
Modal Segue View
I tried to utiize the solution described here, which is supposed to utilize scroll view to shift the view when keyboard appears, but it doesn't seem to work for my project: Move a view up only when the keyboard covers an input field
This question doesn't answer my problem as my text field and view are inside another view to make it appear like a popup to the main screen: Move a view up only when the keyboard covers an input field
In order to implement the pop up view (in a modal segue) I placed the text field and view inside a View which is placed inside a Scroll View which is located inside the view of the Visual Effects with Blur View.
Another problem I'm experiencing is I impleneted the following code to dismiss keyboard when tapping outside the text field/view, but for some reason it doesn't seem to work
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
self.view.endEditing(true)
}
Here is the code for the View Controller of the pop up view in Xcode 9.2
Archive of the project: Xcode Project Archive
//
// LocationEntryScrollViewController.swift
//
import UIKit
class LocationEntryScrollViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate {
#IBOutlet weak var ScrollView: UIScrollView!
#IBOutlet weak var LocationEntryView: UIView!
#IBOutlet weak var LocationTextField: UITextField!
#IBOutlet weak var RemarksTextView: UITextView!
var activeTextField: UITextField?
var activeTextView: UITextView?
override func viewDidLoad()
{
super.viewDidLoad()
registerForKeyboardNotifications()
LocationEntryView.layer.cornerRadius = 10
LocationEntryView.layer.masksToBounds = true
LocationTextField.delegate = self // dismiss keyboard when tapped outside keyboard
RemarksTextView.delegate = self // dismiss keyboard when tapped outside keyboard
}
override func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated)
LocationTextField.becomeFirstResponder()
RemarksTextView.becomeFirstResponder()
}
/**
* Called when the user click on the view (outside the UITextField).
*/
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
self.view.endEditing(true)
}
override func viewDidDisappear(_ animated: Bool)
{
deregisterFromKeyboardNotifications()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func DoneButtonTapped(_ sender: Any)
{
dismiss(animated: true, completion: nil)
}
#IBAction func CancelButtonTapped(_ sender: Any)
{
}
#objc func keyboardWasShown(notification: NSNotification)
{
//Need to calculate keyboard exact size due to Apple suggestions
self.ScrollView.isScrollEnabled = true
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
self.ScrollView.contentInset = contentInsets
self.ScrollView.scrollIndicatorInsets = contentInsets
var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if let activeTextField = self.activeTextField
{
if (!aRect.contains(activeTextField.frame.origin))
{
self.ScrollView.scrollRectToVisible(activeTextField.frame, animated: true)
}
}
if let activeTextView = self.activeTextView
{
if (!aRect.contains(activeTextView.frame.origin))
{
self.ScrollView.scrollRectToVisible(activeTextView.frame, animated: true)
}
}
}
#objc func keyboardWillBeHidden(notification: NSNotification)
{
//Once keyboard disappears, restore original positions
let info : NSDictionary = notification.userInfo! as NSDictionary
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
self.ScrollView.contentInset = contentInsets
self.ScrollView.scrollIndicatorInsets = contentInsets
self.view.endEditing(true)
self.ScrollView.isScrollEnabled = false
}
func textFieldDidBeginEditing(_ textField: UITextField)
{
activeTextField = textField
}
func textFieldDidEndEditing(_ textField: UITextField)
{
activeTextField = nil
}
func textViewDidBeginEditing(_ textView: UITextView)
{
activeTextView = textView
}
func textViewDidEndEditing(_ textView: UITextView) {
activeTextView = nil
}
func registerForKeyboardNotifications()
{
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func deregisterFromKeyboardNotifications()
{
//Removing notifies on keyboard appearing
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
}

Deselect the text in a NSTextField

Is there an easy way to deselect an NSTextField after pressing enter?
First you will need to make your view controller the delegate of your text field. Then you override NSControl instance method controlTextDidEndEditing(_:), get your textfield current editor selected range
and from the main thread set it back to your textfield:
import Cocoa
class ViewController: NSViewController, NSTextFieldDelegate {
#IBOutlet weak var textField: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
override func controlTextDidEndEditing(_ obj: Notification) {
if let selectedRange = textField.currentEditor()?.selectedRange {
DispatchQueue.main.async {
self.textField.currentEditor()?.selectedRange = selectedRange
}
}
}
}
Here is one way I did it.
By disabling it with isSelectable and isEditable and then setting a timer to re-enable it after 0.5s
#IBAction func timeCodeChanged(_: NSTextField) {
timecodeLabel.isSelectable = false
timecodeLabel.isEditable = false
Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(reEnableLabel), userInfo: nil, repeats: false)
}
#objc func reEnableLabel() {
timecodeLabel.isSelectable = true
timecodeLabel.isEditable = true
}

Keyboard not being shown correctly

I have got the following functions that make the keyboard not to cover the TextView, but the keyboard is not showing correctly. Instead, there appears a kind of all black "keyboard" with no keyboard keys.
func textViewDidBeginEditing(_ textView: UITextView) {
moveTextView(textView, moveDistance: -250, up: true)
}
func textViewDidEndEditing(_ textView: UITextView) {
moveTextView(textView, moveDistance: -250, up: false)
}
func textViewShouldReturn(_ textView: UITextView) -> Bool {
textView.resignFirstResponder()
return true
}
func moveTextView(_ textView: UITextView, moveDistance: Int, up: Bool) {
let moveDuration = 0.3
let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)
UIView.beginAnimations("animateTextView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(moveDuration)
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}
Do somebody have an idea of why and how to fix it?
Thank you for your time.
Try to do it another way. Add UITextViewDelegate to your viewController. Add smth like this in viewDidLoad():
self.yourTextView1.delegate = self
self.yourTextView2.delegate = self
//For scrolling the view if keyboard on
NotificationCenter.default.addObserver(self, selector: #selector(YourViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(YourViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
And add this to your ViewController:
var keyBoardAlreadyShowed = false //using this to not let app to scroll view
//if we tapped UITextField and then another UITextField
func keyboardWillShow(notification: NSNotification) {
if !keyBoardAlreadyShowed {
self.view.frame.origin.y -= 50 // we will scroll on it
keyBoardAlreadyShowed = true
}
}
func keyboardWillHide(notification: NSNotification) {
self.view.frame.origin.y += 50
keyBoardAlreadyShowed = false
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
Hope it helps
Try this:
iOS Simulator > Restore Content and Settings.
Clean the Project, and restart Xcode.

Why does double tap gesture only work once on UITextView?

I have a textview that I want to allow the user to edit when they double tap. The double tap works fine when the viewcontroller first loads. After I edit the textview and close the textview then reopens for editing with just a single tap. My code is:
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var textView: UITextView!
let myTaps = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textView.gestureRecognizers = nil
myTaps.addTarget(self, action: "handleTaps:")
myTaps.numberOfTapsRequired = 2
myTaps.numberOfTouchesRequired = 1
textView.addGestureRecognizer(myTaps)
}
func handleTaps(recognizer: UITapGestureRecognizer) {
print("Taps")
textView.becomeFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
view.endEditing(true)
super.touchesBegan(touches as Set<UITouch>, withEvent: event)
}
func textViewShouldEndEditing(textView: UITextView) -> Bool {
textView.endEditing(true)
return true
}
}

dismiss keyboard with a uiTextView

I am sure this is not that difficult, but I am having trouble finding info on how to dismiss a keyboard with the return/done key using a textview, not a textfield. here is what I have tried so far(which works with a textfield.)
Thanks very much in advance for any help!
// PostTravelQuestion.swift
class PostTravelQuestion: UIViewController, UITextViewDelegate {
#IBAction func closepostpage(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
#IBOutlet var postquestion: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
postquestion.delegate = self
}
self addDoneToolBarToKeyboard:self.textView
/*func textViewShouldEndEditing(textView: UITextView) -> Bool {
textView.resignFirstResponder()
return true
}*/
/*override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
postquestion.resignFirstResponder()
self.view.endEditing(true)
}*/
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textViewShouldReturn(textView: UITextView!) -> Bool {
self.view.endEditing(true);
return true;
}
}
This works for me:
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
}
/* Updated for Swift 4 */
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
/* Older versions of Swift */
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
}
Add UITextViewDelegate to your class and then set your delegate for your textView or your textField in viewDidLoad. Should look something like this:
// in viewDidLoad
textField.delegate = self
textView.delegate = self
Swift 3
// hides text views
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
// hides text fields
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if (string == "\n") {
textField.resignFirstResponder()
return false
}
return true
}
Swift 2.0
The below syntax has been tested for Swift 1.2 & Swift 2.0
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
Below code will dismissing the keyboard when click return/done key on UITextView.
In Swift 3.0
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
{
if(text == "\n")
{
view.endEditing(true)
return false
}
else
{
return true
}
}
In Swift 2.2
func textView(textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
{
if text == "\n"
{
view.endEditing(true)
return false
}
else
{
return true
}
}
Easiest and best way to do this using UITextView Extension.
Credit: http://www.swiftdevcenter.com/uitextview-dismiss-keyboard-swift/
Your ViewController Class
class ViewController: UIViewController {
#IBOutlet weak var myTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// 1
self.myTextView.addDoneButton(title: "Done", target: self, selector: #selector(tapDone(sender:)))
}
// 2
#objc func tapDone(sender: Any) {
self.view.endEditing(true)
}
}
Add UITextView Extension
extension UITextView {
func addDoneButton(title: String, target: Any, selector: Selector) {
let toolBar = UIToolbar(frame: CGRect(x: 0.0,
y: 0.0,
width: UIScreen.main.bounds.size.width,
height: 44.0))//1
let flexible = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)//2
let barButton = UIBarButtonItem(title: title, style: .plain, target: target, action: selector)//3
toolBar.setItems([flexible, barButton], animated: false)//4
self.inputAccessoryView = toolBar//5
}
}
For more detail: visit full documentation
to hide the keyboard touch on any part outside the textbox or textviews in swift 4 use this peace of code in the ViewController class:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
super.touchesBegan(touches, with event: event)
}
Regards
Building on the answers of others (kudos!), here is my minimalistic take on it:
import UIKit
extension UITextView {
func withDoneButton(toolBarHeight: CGFloat = 44) {
guard UIDevice.current.userInterfaceIdiom == .phone else {
print("Adding Done button to the keyboard makes sense only on iPhones")
return
}
let toolBar = UIToolbar(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: toolBarHeight))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(endEditing))
toolBar.setItems([flexibleSpace, doneButton], animated: false)
inputAccessoryView = toolBar
}
}
Usage:
override func viewDidLoad() {
super.viewDidLoad()
textView.withDoneButton()
}