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)
}
Related
I am implementing keyboard close as follows but it doesn't work.
extension SleepDiaryViewController: UITextFieldDelegate {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
view.endEditing(true)
}
}
Any ideas why this isn't working?
I have these buttons on my cameraView that I want to hide when pressing on the view. I got that to work but I want to unhide the buttons when I press on the view again. How would I be able to do that?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if touch?.view == self.cameraView {
flipCamera.isHidden = true
lockButton.isHidden = true
print("Hide buttons")
} else if touch?.view == self.cameraView && flipCamera.isHidden == true {
print("show buttons")
}
If you want to change the isHidden value to its opposite on each touch, you can simply use the toggle() function, which toggles a Bool value - it assigns false if the value was true and assigns true if the value was false.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if touch?.view == self.cameraView {
flipCamera.isHidden.toggle()
lockButton.isHidden.toggle()
}
}
How could i make the text disappeared when the user tap the screen when UIscrollview is active
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
I'm using the typical func for get and dismiss the keyboard
I try to
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
UIScrollView.endEditing(true)
}
This in viewdidLoad ()
let scrollViewTap = UITapGestureRecognizer(target: self, action: #selector(self.scrollViewTapped))
scrollViewTap.numberOfTapsRequired = 1
scroll_view.addGestureRecognizer(scrollViewTap)
and This in class
#objc func scrollViewTapped() {
scroll_view.endEditing(true)
self.view.endEditing(true) // anyone
}
in ViewDidLoad
let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.touch))
recognizer.numberOfTapsRequired = 1
recognizer.numberOfTouchesRequired = 1
elskrolll.addGestureRecognizer(recognizer)
elskroll must be your UIScrollView name:
outside the ViewDidLoad
#objc func touch() {
//print("Touches")
self.view.endEditing(true)
}
When I tap on my table view it will trigger touchesBegan and touchesShouldBegin. However, I also have a UIPanGestureRecognizer on my view cells and when panning a cell, tapping on other cells won't trigger touchesShouldBegin.
The reason I need touchesShouldBegin is because I want to stop the touches when a cell is being panned. Is there other methods I can use to do this? I have tried having set allowsMultipleSelection to false and true.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("This will always be printed")
}
override func touchesShouldBegin(_ touches: Set<UITouch>, with event: UIEvent?, in view: UIView) -> Bool {
print("This will only be printed when no cells are being panned")
return true
}
Try to set the UITableView's delaysContentTouches property to true.
You can also try to add the a UIGestureRecognizerDelegate to your UIPanGestureRecognizer and have :
extension MyViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
I have a UITextView with a label over it as a placeholder. When the user taps on the UITextView the label disappears but for the keyboard to appear it takes another 2 taps. When I remove the tap gesture that hides the label the keyboard works perfectly. Here is my code any ideas as to what the problem is???
var tapTerm:UITapGestureRecognizer = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
bioTextfield.delegate = self
tapTerm = UITapGestureRecognizer(target: self, action: "tapTextView:")
// bioPlaceholderLabel.addGestureRecognizer(tapTerm)
bioTextfield.addGestureRecognizer(tapTerm)
}
func tapTextView(sender:UITapGestureRecognizer) {
// hide placeholder label text
bioPlaceholderLabel.text = ""
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// when user touches outside the keyboard close the keyboard
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
// when user presses the return button close the keyboard
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
return false
}
return true
}
Any help would be greatly appreciated!
Easier way, you should use this custom UITextView (with place holder): KMPlaceholderTextView. I'm using, so great.