In Messages.app you can dismiss the keyboard down by scrolling the list view. To be clear, it isn't simply responding to a scrollViewDidScroll event. The keyboard tracks with your finger as you swipe down. Any idea how this is done?
Since iOS 7, you can use
scrollView.keyboardDismissMode = .Interactive
From the documentation:
UIScrollViewKeyboardDismissModeInteractive
The keyboard follows the
dragging touch offscreen, and can be pulled upward again to cancel the
dismiss.
In the XCode, attributes inspector, the scrollView has a Keyboard attribute. It has 3 options.
Do not dismiss
Dismiss on drag
Dismiss interactive.
If you're using a tableView and Swift 3 or Swift 4, it works by using:
tableView.keyboardDismissMode = .onDrag
Since iOS7, UIScrollView and all classes that inherit from it (including UITableView) have a keyboardDismissMode property. With Swift 5 and iOS 12, keyboardDismissMode has the following declaration:
var keyboardDismissMode: UIScrollView.KeyboardDismissMode { get set }
The manner in which the keyboard is dismissed when a drag begins in the scroll view.
Note that UIScrollView.KeyboardDismissMode is an enum that has none, interactive and onDrag cases.
#1. Set keyboardDismissMode programmatically
The code snippet below shows a possible implementation of keyboardDismissMode:
override func viewDidLoad() {
super.viewDidLoad()
// Dismiss keyboard when scrolling the tableView
tableView.keyboardDismissMode = UIScrollView.KeyboardDismissMode.interactive
/* ... */
}
#2. Set keyboardDismissMode in storyboard
As an alternative to the programmatic approach above, you can set the keyboardDismissMode value for your UIScrollView/UITableView in the storyboard.
Select your UIScrollView / UITableView instance,
Select the Attributes Inspector,
Set the correct value for Keyboard.
Without tableview - yes it not a swipe but it doesn't the trick
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
Related
I have a layout with a UIView at the top of the page and, right below it, I have a UITableView.
What I am wanting to do is to transfer the gesture interactions on the UIView to the UITableView, so when the user makes a drag up/down on the UIView, the UITableView scrolls vertically.
I tried the following code
tableView.gestureRecognizers?.forEach { uiView.addGestureRecognizer($0) }
but it removed the gestureRecognizers from the UITableView somehow :/
Obs.: the UIView cannot be a Header of the UIScrollView
That's Tricky
What is problem ?
Your top view is not allowed to pass through view behind it...
What would be possible solutions
pass all touches to view behind it (Seems to not possible or very tough practically )
Tell window to ignore touches on top view (Easy one)
Second option is better and easy.
So What you need to do is create subclass of UIView and override
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
and return nil if you found same view on hitTest action
Here Tested and working example
class PassThroughME : UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return super.hitTest(point, with: event) == self ? nil : self
}
}
That's it now use PassThroughME either by adding class to your view in storyboard or programmatically whatever way you have added your view
Check image i have black color view with 0.7 alpha on top still i am able to scroll
Hope it is helpful
I am working on a tiny project where I want to use the 3D touch screen pressure capabilities (e.g. touch.force).
Right now I can measure force in my ViewController and it is behaving like I want it:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if traitCollection.forceTouchCapability == UIForceTouchCapability.available {
// 3D Touch capable
let force = touch.force
print("Force: " + force.description)
}
}
}
I have one UIImageView that presents a picture. I also use an custom UICollectionView as overlay, which contains transparent cells and colored cells. What I am trying to achieve is to measure both force, and be able to remove the color from the cells when they are pressed, but I am failing to do both. I can either measure force, or change the color of a cell in the UICollectionView, but not both.
The solution as I see it is to forward touches from the UICollectionView to the next view in the hierarchy. But another solution could be to measure both force and register taps on cells from within the custom UICollectionView.
My question is, is either of my proposed solutions any good? And if so, how do I achieve them?
Apparently the override func touches... methods are part of UIView, not neccesarily UIViewControllers. This means that implementing the touchesMoved and other methods into my custom UICollectionView is perfectly possible and it solved my problem of handling touches and taps at the same time.
I have a custom button and it's added to a static cell. Somehow whenever I tap the button, it doesn't react immediately like when the button is added to a plain normal view.
When tapped, it registers the tap right away but the background color has a slight delay before changing.
To change the color I have the following code in my subclass of UIButton:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
border.fillColor = .redColor() // border is a CAShapeLayer
print("began") // This prints right when the button is clicked
}
This button is added in a subclass of UITableViewCell and the button is reacting, except changing the color has a delay. How can I fix this?
Answer (by Jelly):
The answer of Jelly helped me a lot and made the button react a bit better. Knowing what the problem is also made it easier to research the problem and found out to make the button react just as it would in a normal view I have to disable both delaysContentTouches in the table view and it's subviews:
tableView.delaysContentTouches = false
tableView.subviews.forEach { ($0 as? UIScrollView)?.delaysContentTouches = false }
Set delaysContentTouches = false on your tableView.
I usually use this function to hide the keyboard once any point in screen is touched
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
But when I use it with scroll view it doesn't work.
How I can hide the keyboard once any point of scroll view is touched ?
Try set keyboardDismissMode of UIScrollView to OnDrag or Interactive, it's default to UIScrollViewKeyboardDismissModeNone
The manner in which the keyboard is dismissed when a drag begins in the scroll view.
Add tap gesture recognizer to scrollView object.
let touch = UITapGestureRecognizer(target: self, action: "singleTapGestureCaptured:")
scrollView.addGestureRecognizer(touch)
and receive the touch event and hide keyboard.
func singleTapGestureCaptured(gesture: UITapGestureRecognizer){
self.view.endEditing(true)
}
There is delegate function for scrollview - "scrollview did scroll" you can dismiss the keyboard there.
I have a custom subclass of UIView, designed in IB that contains a few labels and a button.
There is an action for touchUpInside event on the button and the target is the custom view.
I am attaching this custom view to a self.tableView.tableHeaderView for a tableview in my UI.
The strange thing is this custom view is not responding to touches. I can see it nicely with all the labels and the button in side the table view, that means the table view handles and shows it correctly, however it is not responding to touches.
I checked the whole view hierarchy and all the views involved have userInteractionEnabled as YES.
If i drag some other controls into that custom view for example a switch, segmented control..they do not respond as well. It is like these controls in custom view are not registering touches.
It doesn't make any sense. I am out of ideas. Can you help to allow the touch event on the button to arrive to its parent view?
What is a "headerTableView"? Do you mean a UITableViewHeaderFooterView? Have you tried setting userInteractionEnabled on the root UITableViewHeaderFooterView?
This is a hack that will detect a button touch and trigger touchUpInside programmatically:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
super.touchesBegan(touches, with: event)
if let touch = touches.first, button.bounds.contains(touch.location(in: button)) {
button.sendActions(for: UIControl.Event.touchUpInside)
}
}
I don’t know what your real problem is because that button should work without this.