How to handle background tap in UICollectionView
This is how I tried to set gesture recognizer to background view. I managed to get separate events.
self.collectionView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
self.collectionView.backgroundView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
self.collectionView.backgroundView.gestureRecognizers = @[tapRecognizer];
iOS - Dismiss keyboard when touching outside of UITextField
You'll need to add an UITapGestureRecogniser
and assign it to the view, and then call resign first responder on the UITextField
on it's selector.
The code:
In viewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
In dismissKeyboard:
-(void)dismissKeyboard
{
[aTextField resignFirstResponder];
}
(Where aTextField
is the textfield that is responsible for the keyboard)
Swift 3 version looks like that
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard (_:)))
self.view.addGestureRecognizer(tapGesture)
For dismissKeyboard
@objc func dismissKeyboard (_ sender: UITapGestureRecognizer) {
aTextField.resignFirstResponder()
}
Related Topics
Swift 3 - Passing Data Between a View Controller and After That to Another 2
Create Alert Function in All View Controllers - Swift
How to Catch Nsunknownkeyexception in Swift 2.2
Problems with Cropping a Uiimage in Swift
Post Parameter to Sever Using Dictionary Swift
Ios11 Uibarbuttonitem Not Working
Adding Firebase Data to an Array in iOS Swift
Swiftui Animation and Subsequent Reverse Animation to Original State
Add View as a Parameter to a Custom Viewmodifier
Game Exits from Pause State After Resuming It from Background in Swift
iOS Development App Startup Crash
How to Pass a Swift Object to JavaScript (Wkwebview/Swift)
Your Credentials Do Not Allow Access to This Resource Twitter API Error
Mysterious "Cryptographic Verification Failure" Error on MACos Sierra, Xcode 8
How to Rotate Custom Userlocationannotationview Image Using Mapkit in Swift