I try to hide keyboard by pressing on any place of the screen. I use following code
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
But id doens't work, keyboard is not hiding.
How to use it correctly ?
Conform to UITextFieldDelegate
In your viewController() assign delegate of YOURTextField.delegate = self.
In your method use self.YOURTextField.resignFirstResponder()
Related
I keep having an issue with a textfield I have set as a decimal keyboard, because its the decimal keyboard it doesn't have a return key so I have to tap background to dismiss it. I want to only allow it to recognize that tap if the keyboard is open. Currently it recognizes it anytime you tap the background. Any help would be awesome thanks.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
label.text = "tapped"
}
You can add this extension, it worked for me:
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)
}
}
Then put this self.hideKeyboardWhenTappedAround() in viewDidLoad
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
I am relatively new to Swift and I am following some basic tutorials but I seem to be having a problem with some methods which attempt to allow the user to press return to minimise the keyboard or to click off the keyboard and the keyboard will disappear, I understand why I am receiving these errors but have no clue how to go about fixing it, I feel something may have been changed in the newer version of Swift I am using as he is using an older version than me, could anyone possibly explain how to go about fixing these two errors please? Any help would be greatly appreciated here is my source code: (First error, value of type 'viewController' has no member 'text' and secondly, touchesBegan method does not override any method from its superclass)
import UIKit
class ViewController: UIViewController {
#IBAction func buttonPressed(sender: AnyObject) {
label.text = textArea.text
}
#IBOutlet weak var textArea: UITextField!
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.text.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder()
return true
}
}
You have 2 problems here, based on the images you posted:
1) The method touhesBegan you are using is not correct:
Correct one:
func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?)
Yours:
func touchesBegan(touches: NSSet, withEvent event: UIEvent)
I think you want a delegate for the UITextField, so this one is not corerct: touchesBegan is a method for the UIReponder delegate and not for UITextFieldDelegate.
Here you can find the reference for the UITextFieldDelegate.
2) the variable text doesn't exists in your code. I think you wanted to use textArea instead.
Hope this can help you, happy coding!
In your case change following thing:
instead of :
self.text.delegate = self
change :
self.textArea.delegate = self
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
And for delegate add like this
class ViewController: UIViewController, UITextFieldDelegate {
}
I want to track down the position of my fingers in a UIViewController. I implemented this with the following function and this is working great!
override func viewDidLoad() {
view.userInteractionEnabled = true
view.multipleTouchEnabled = true
super.viewDidLoad()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//Create subview and place it under your finger.
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
//Here methods which tracks the moves of the finger and updates the subviews position.
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
//Remove subview from superview overhere.
}
I have two issues:
When I press more than 5 fingers on the screen, touchesBegin/touchesMoved/touchesEnded will not be called anymore. Even when I place 6 fingers and remove 1.
Sometimes touchesBegan is called from random positions on my screen. TouchesEnded is called for the same point after that.
Should I implement this on another way? I also tried it using UIGestureRecognizer but without success.
I added this function
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
}
And this is called when I tap the 6th finger on the screen.
In my SKScene subclass I have implemented a touchesBegan method. This method had the NSSet changed to Set in order to make it Swift 1.2 compatible (see this question).
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// ...
}
Now the compiler gives me an error: Method does not override any method from its superclass. My code -as any Swift code- was broken in 1.2, and I have fixed every issue except this override case. Am I missing something here?
This worked for me
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//...
}
For more information about why is happening this error you can read from this answer that explain in detail about this: https://stackoverflow.com/a/30892467/2091181
So the "problem" had everything to do with the fact that the project had a Set class in it implemented, and I did not detect this redundancy. Quite a silly mistake.
I have a UITextView inside a UITableViewCell in a table. "editable" for the UITextView is turned off, which allows me to set dataDetectorTypes to UIDataDetectorTypeAll, which is exactly what I want. The app now detects when the user touches a link in the UITextView, and does the appropriate thing.
The problem arises when the user touches on part of the UITextView where there is no link. I want the didSelectRowAtIndexPath in the UITableView delegate to be called. But it isn't, because the UITextView is trapping the touch, even when no link is detected.
My first guess was to turn userInteractionEnabled on the UITextView to NO. This means that didSelectRowAtIndexPath will get called, but then the UITextView can't detect links. It's a catch-22.
Any ideas on how to fix this?
Thanks for any help.
Maybe you could try passing the touch up the responder chain.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
}
Override all four of the UIResponder touch handlers to forward to the text view's superview.
The header file states that "Generally, all responders which do custom touch handling should override all four of these methods. … You must handle cancelled touches to ensure correct behavior in your application. Failure to do so is very likely to lead to incorrect behavior or crashes."
class MyTextView: UITextView {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.superview?.touchesBegan(touches, withEvent: event)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.superview?.touchesMoved(touches, withEvent: event)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.superview?.touchesEnded(touches, withEvent: event)
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
self.superview?.touchesCancelled(touches, withEvent: event)
}
}