dismiss keyboard tapping background with action - swift

My app has two UITextFields. Both I have dismissing by tapping the background because they are using the decimal keyboard. I want it so if say textField1 is up and you tap the background a specific button pops up with the actions for textField1 and vice versa. Currently if you tap the background with out any keyboard up it still pops up a button. Any help would be awesome thanks.
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var button: UIButton!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
button.isHidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
self.textField.delegate = self
button.isHidden = true
}

Related

UIImageView Zoom and Touch Image

I have a big image and I want to show this image in ScrollView and I want to detect touched area with x and y coordinates of the image.
If I choose 'User Interaction Enabled' of ScrollView, zoom is working but touchesBegan method doesn't working on ScrollView(It's working outside),
If I didn't choose 'User Interaction Enabled' of ScrollView, zoom isn't working but touchesBegan method working on ScrollView and I'm getting coordinates but I can't zoom.
How can I solve this problem ?
Code :
class ViewController: UIViewController,UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
self.scrollView.minimumZoomScale=1.0;
self.scrollView.isScrollEnabled = true
self.scrollView.maximumZoomScale=7.0;
self.scrollView.contentSize=CGSize(width: 1424,height: 1410);
self.scrollView.delegate=self;
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.imageView
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: self.imageView)
print(position.x)
print(position.y)
}
}

Dismiss UITextField keyboard?

I have multiple textfields
#IBOutlet var emailLogin: UITextField!
#IBOutlet var passLogin: UITextField!
#IBOutlet var emailSignUp: UITextField!
#IBOutlet var passSignUp: UITextField!
as of now these aren't really needed, because of how I am dismissing it by tapping anywhere on the screen, however I also want it to dismiss when I press the return key.
override func viewDidLoad() {
super.viewDidLoad()
self.emailLogin.delegate = self
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
First question: When I need to dismiss with only one I would set the delegate like so, but how do I handle this when I have multiple views that need to be dismissed on return key?
Also, there are two separate views, but both use the same class. Is this a problem for what I am trying to do?
From here
func dismissKeyboard() {
view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
This dismisses the keyboard but only for the textfield that I set as self.
Any info is appreciated!
EDIT:
Change your code to the following and it should work:
#IBOutlet var emailLogin: UITextField?
#IBOutlet var passLogin: UITextField?
#IBOutlet var emailSignUp: UITextField?
#IBOutlet var passSignUp: UITextField?
emailLogin?.delegate = self
passLogin?.delegate = self
emailSignUp?.delegate = self
passSignUp?.delegate = self
The IBOutlets from other class were not initialized when your LoginViewController loads, thus end up with unwrapping nil objects, which is not allowed in Swift. (You should be able to see that in your console output in Xcode.) Use optional variables will prevent that from happening.
Use ViewControllerDelegate to handle tap outside of any textField and textView. It will dismiss keyboard when you tap on outside of textField. Put below code in your view controller:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent:event)
self.view.endEditing(true)
}
You don't need to add TapGesture now to handle this.

UIPanGestureRecognizer does not work

It's weird, but when I add a UIPanGestureRecognizer to a view it does not work. It's my view, that white view is a subview of the red view:
Main View:
#IBOutlet weak var redView: UIView!
#IBOutlet weak var whiteView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let panRecognizer = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan))
whiteView.gestureRecognizers = [panRecognizer]
}
func handlePan(recognizer:UIPanGestureRecognizer) {
//foo
}
When I tap and drag the white view it won't move.
try this:
whiteView.isUserInteractionEnabled = true
From the Apple's docs:
If the code for your pan gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed:
The isUserInteractionEnabled property of the view is set to true. Image views and labels set this property to false by default.
The number of touches is between the values specified in the minimumNumberOfTouches and maximumNumberOfTouches properties.
For a UIScreenEdgePanGestureRecognizer object, the edges property is configured and touches start at the appropriate edge.
Handling Pan Gestures
Here is the answer:
class ViewController: UIViewController {
#IBOutlet weak var redView: UIView!
#IBOutlet weak var whiteView: UIView!
var lastLocation:CGPoint = CGPointMake(0, 0)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let panRecognizer = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan))
whiteView.addGestureRecognizer(panRecognizer)
}
func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(redView)
whiteView.center = CGPointMake(lastLocation.x + translation.x, lastLocation.y + translation.y)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Promote the touched view
lastLocation = whiteView.center
}
}
If you are using swift language for method you can't use [].
var PanGesture = UIPanGestureRecognizer(target: self, action: "respondToPanGesture:")
whiteView.addGestureRecognizer(PanGesture)
Hope it helps.

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
}
}

in swift how to dismiss the key board

I am wondering how to dismiss a key board if the user touches outside the UITextField. I am using Xcode 6.1. I added a UITextField to a UIViewController as per the below thru ViewDidLoad() function. Any help on dismissing the key board would be much appreciated.
class PickerdemoViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate{
var textBox1 : UITextField!
override func viewDidLoad() {
//..................adding text box.........................//
self.textBox1 = UITextField (frame: CGRectMake(100, 152.5, 50, 35))
textBox1.delegate = self
textBox1.backgroundColor = UIColor.whiteColor()
textBox1.placeholder = "enter"
textBox1.keyboardType = UIKeyboardType.DecimalPad
self.textBox1.resignFirstResponder()
textBox1.keyboardAppearance = UIKeyboardAppearance.Default
self.view.addSubview(textBox1)
super.viewDidLoad()
}
You need to have a reference to the UITextField so make a property value like this
class MyClass: UIViewController {
var textBox1: UITextField!
...
// create your textfield where ever you were by assigning it to self.textBox1
}
Then to dismiss the keyboard you resign its as the first responder.
self.textBox1.resignFirstResponder()
Update to dimiss keyboard
Dismissing on return/done with the textField delegate method
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.textBox1.resignFirstResponder()
return true
}
Dismissing on a button click (custom IBAction method)
#IBAction func buttonClicked(sender: UIButton!) {
self.textBox1.resignFirstResponder()
}
This will dismiss the keyboard by tapping screen. Make sure to not put it in the viewDidLoad.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { // this func lets you close keyboard by touching screen
self.view.endEditing(true) // this line lets you close keyboard by touching screen
}