The scrollview takes up the whole viewcontroller. A textview is placed in the scrollview. I wish to dismiss the keyboard when the use taps outside of the keyboard.
boostContent is the IBOutlet of the textview.
I've tried the code below and it doesn't work.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.scrollView.endEditing(true)
boostContent.resignFirstResponder()
}
What's the correct solution?
I've also tried self.view.endEditing(true)
Add a Tap Gesture Recognizer to the same view holding your text view. Wire it up to this:
#IBAction func tapped(sender: AnyObject) {
boostContent.resignFirstResponder()
}
I think the issue with what you were trying to do is that your touchesBegan wasn't being called because it is defined on your ViewController and the content view of the scroll view is intercepting the touches.
One way is to add a tap gesture to your view controller and resign the first responder there. The other way, and I think is a better one, is to use a container view. You put it inside you scrollview and then put all your objects, including the textfield, in the container view.
Related
I've created UIViewController with UIScrollView in it. In UIScrollView I added some UITextFields for data input. When one of the UITextFields becomes firstResponder (keyboard appeared on the screen) and I'm trying to pop this UIViewController with a swipe gesture, I have the following effect:
View of the UIViewController is getting down and I may see the part of previous UIViewController in current UIViewController. Do you have any ideas, how to solve this?
I have found out that when using custom back actions, the interactive pop gesture stops working.
To fix this you can set the interactivePopGestureRecognizer.delegate property to nil.
Try this:
class NavigationController: UINavigationController, UIGestureRecognizerDelegate {
/// Custom back buttons disable the interactive pop animation
/// To enable it back we set the recognizer to `self`
override func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}
I have changed my view to a UIControl in order to add a TouchDown recognizer (that way I can dismiss isEditing in my UITextView). I have also added an outlet to a function in my class. All my other outlets work, but when I touch down this never gets called. Could it be because my Control has many things layered on top of it? Any other idea why?
#IBAction func userTappedBackground(_ sender: Any) {
bioTextView.endEditing(true)
}
I am trying to implement a very basic chat feature into my app and I am using constraints to keep everything in the correct place. It is great except for when I need to actually type, and the problem that arises is that the keyboard covers the text field and I not only cannot see the textfield but I cannot dismiss it. Thank you for all help!
In summary,
Using a textfield with contraints at bottom of screen
keyboard shows up and covers it, and I cannot dismiss the keyboard
Just set observers for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification.
Whenever, UIKeyboardWillShowNotification is triggered, move the UITextfield upwards equivalent to the keyboard height. Then, when the UIKeyboardWillHideNotification is triggered, move the keyboard back into place.
Dismiss keyboard by tapping anywhere
override func viewDidLoad()
{
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)}
func dismissKeyboard()
{
view.endEditing(true)
}
I have View Controller that accepts text input using an UITextView called "myTextView".
When I segue to myTextView, I use the following:
self.performSegueWithIdentifier("myTextView", sender: self)
myTextView is presented. Then next the keyboard is displayed after viewDidAppear:
override func viewDidAppear(animated: Bool) {
self.myTyping.becomeFirstResponder()
}
Everything animates smoothly. However I want the keyboard to display at the same time as the segue animating and being displayed together and not one after the other.
I can do that with the call for the keyboard with the segue call. However, the animation is jittery and choppy, and not smooth.
How can I both segue to myTextView View Controller and display the keyboard at the same time without any animation jitters?
I have a UIPageViewController in it have I have 4 pages. One of my pages have UISegmentControl where the user either tap or swipe the page to change segments. I think it is inside UIPageController so swipe takes to different page rather than swipe action in UISegmentControl or might be some silly mistake on my part. here's what I have done. I have added two gesture recognizers swipe Left and swipe right. and done this in storyboard
Two solution:
1.Handle all swipe gestures in UIPageViewController.If you don't want to change your detail view controller,you can add a public func to change some values.In UIPageViewControllerDataSource you can return any view controller you want to present.
2.Use UIGestureRecognizerDelegate.If you don't want get this gesture,just return NO/false.(set delegate for gesture in UIPageViewController)
// get gesture recognizer and set delegate
self.pageViewController!.gestureRecognizers
// decide if page controller should receive gesture
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool