is how my screen looks like..
Now my code for dismissing the keyboard when the use of searchbar is over:
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
view.addGestureRecognizer(tap)
}
#objc func handleTap() {
searchbar.resignFirstResponder()
}
By this my keyboard is getting dismissed but whenever using this, the tableview DidSelectRowAt is no more working (Though there is no error or warning). I am confused that what is the problem exactly. Please help! Thanks is advance..
Set cancelsTouchesInView to false, for detailed explanation: link
extension UIViewController {
func hideKeyboard() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
#objc
func dismissKeyboard() {
view.endEditing(true)
}
}
Usage:
call hideKeyboard() in the viewDidLoad of the controller.
Related
I have UITextField and UIButton in my application.
To hide system keyboard which is shown when UITextField is clicked, I added UITapGestureRecognizer to my view.
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(didTap(_:)))
view.addGestureRecognizer(tapGesture)
}
#objc func didTap(_ recognizer: UIGestureRecognizer) {
view.endEditing(true)
}
#IBAction func onClickedButton(_ sender: Any) {
print("aaa")
}
This code worked very well when I touched outside of my button.
However, when I clicked the button which has IBAction(onClickedButton), the keyboard did not disappear and only the message "aaa" printed in output console.
What I want to do is to hide keyboard and invoke IBAction at the same time. In other words, I want to invoke Tap gesture and IBAction at the same time, when I clicked my button.
How can I acheive this?
I found the solution.
Just setting
tapGesture.cancelsTouchesInView = false
can acheive this.
By doing like this, tapGesture hides keyboard and after that, passes touch event to the UIButton.
You can just add view.endEditing(true) to your onClickedButton.
try this
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(didTap(_:)))
view.addGestureRecognizer(tapGesture)
}
#objc func didTap(_ recognizer: UIGestureRecognizer) {
view.endEditing(true)
}
#IBAction func onClickedButton(_ sender: Any) {
print("aaa")
view.endEditing(true)
}
}
I'm building an application in Swift 3, so I want to call a function if I click on a particular UILabel, so I'm write this code but not works:
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tap)
How can I render UILabel clickable ?
Set user interaction enabled for the UILabel and add the below code in the viewDidLoad()
self.label.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(self.labelTapped))
self.label.addGestureRecognizer(tap)
Add the tap action function as below :
#objc func labelTapped(_ gestureRecognizer: UITapGestureRecognizer) {
print("Label clicked")
}
Please make user that there is no other transparent view overlapping the UILabel in the view. If the UILabel is a part of another view then please make sure that the container View's user interaction is enabled.
Hope this helps.
Your selector should be an #objc func within self.
<#YourLabel#>.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleLabelTap)))
And when the user taps the label it will trigger:
#objc func handleLabelTap() {
// handle label tap here
}
You are lacking a function to trigger when the gesture touch is recognized. You need to add following:
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction(_:)))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tap)
#objc func tapFunction(_ gestureRecognizer: UITapGestureRecognizer) {
// handle label tap here
}
Please ensure that You have connected the outlet to UILabel because I have created simple demo code by copy-paste your code and it is worked as expected.
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tap)
}
#objc func tapFunction() {
print("tapFunction")
}
I suggest, Please remove UILabel from UIViewController and add it again.
Download sample code
Note: - Please ensure that user-interaction of UILabelis enabled
First you need to add Tap Gesture into storyboard
Create Action of that particular gesture
override func viewDidLoad() {
super.viewDidLoad()
let tapOnLabel = UITapGestureRecognizer(target: self, action: #selector(self.tapGestireAction))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tapOnLabel)
}
#IBAction func tapGestureAction(_ sender: UITapGestureRecognizer) {
//Perform action
}
Hi i want to add tap gesture on UILabel inside a UITableViewCell . here i have implemented the code like this.
override func awakeFromNib()
{
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action:
#selector(self.tapFunction))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
}
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
but it is giving me error like this
fatal error: unexpectedly found nil while unwrapping an Optional value
Can anyone tell me what is wrong in this?
This should be
let tap = UITapGestureRecognizer(target: self, action:#selector(self.tapFunction(_:)))
If you use Swift 4 add #objc
#objc func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
its working now with the same code . Just i deleted the cell and created a new UITableviewCell. i guess it may be xcode bug
override func awakeFromNib()
{
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action:
#selector(self.tapFunction))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
}
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
I am using this library for SearchText Field
https://github.com/apasccon/SearchTextField
Here is my Code :
override func viewDidLoad() {
super.viewDidLoad()
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector("dismissKeyboard")))
}
func dismissKeyboard()
{
ClientList.resignFirstResponder()
self.view.endEditing(true)
}
Dismiss of keyboard is working but the didSelectRowAtIndexPath is not getting called so i am not able to select anything
Image for Reference :
Partial Solution I Done:
func hideKeyboard(gestureRecognizer: UIGestureRecognizer) {
let point = gestureRecognizer.locationInView(ClientList.tableView)
let indexPath = ClientList.tableView!.indexPathForRowAtPoint(point)
if indexPath != nil {
return
}
if ClientList.becomeFirstResponder() {
ClientList.resignFirstResponder()
}
}
But now issue is i have other list too , how can i implement for other list ?
You can solve this issue like this
ViewDidLoad
self.tap = UITapGestureRecognizer(target: self, action: "viewTapped:")
self.tap.delegate = self
self.view.addGestureRecognizer(self.tap)
Function
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if touch.view != nil && touch.view!.isDescendantOfView(self.tableView) {
return false
}
return true
}
Who's the target of the gesture? I think it should be the list since it is what you tap and it is above the view
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
Add this to viewDidLoad
And add this function
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
tapGesture.cancelsTouchesInView = true
scrlView.addGestureRecognizer(tapGesture)
//self.navigationController!.navigationBarHidden = true
scrlView.contentSize = CGSizeMake(UIScreen .mainScreen().bounds.size.width, (regButton.frame.size.height + regButton.frame.origin.y)+20)
// Do any additional setup after loading the view.
}
func hideKeyboard() {
scrlView.endEditing(true)
scrlView .setContentOffset(CGPointMake(0, 0), animated: true)
}
#IBAction func tapped(sender: AnyObject) {
scrlView.endEditing(true)
scrlView .setContentOffset(CGPointMake(0, 0), animated: true)
}
My hideKeyboard function is ok. But now I want to cahnge the position of the textfields above the keyboard when I tap on them.
try this :
func textFieldShouldBeginEditing(textField: UITextField) -> Bool{
scrl.setContentOffset(CGPointMake(0.0, textField.center.y-120), animated: true)
}
And your func hideKeyboard() should be remain same
Hope it will helpful for u
You can use IQkeyboardmanger third party library for your project, here is the link for that, https://github.com/hackiftekhar/IQKeyboardManager