Resign first responder after touch on contentview - ios

Here's my setup:
I have a view containing a small view at the bottom of the screen, which contains a text field. I have added some logic to move the whole view (including the small one) up as soon as the textbox is selected, and down as soon as the textbox disappears.
I have also added a gesturerecognizer to the whole thing, to resign first responder once the user taps somewhere else.
Afterwards, I added a ContentView to all of this, which in turn references a tableviewcontroller. It looks fine: a tableview in the back, the text field in its view at the bottom, once I tap the textfield everything is moved up and down as intended.
However, the gesturerecognizer doesnt work properly. It detects touches to the small view containing the textfield, but not on the tableview.
I have tried adding the gesturerecognizer to the tableview, but it didnt seem to make a difference - the gestures weren't recognized. I also tried adding another view on top of the ContentView - it worked, but it didn't pass the events to the tableview below.
I have created the views with storyboard and added the code to move the views programatically.

The gesture recognizer is working properly, make sure that it doesn't conflict with other event handlers. You can put break point into the method which is handling events to make sure it does really handles those events you wish.
As the alternative, put a blank view in front of table view and attach gesture recognizer. :)

I solved it using the answer provided [here]. I was not able to solve it with the storyboard gesture recognizer without additional code.1

Related

Swift canEditRowAtIndexPath not working in UIPageViewController EZSwipeViewController

I have the following scenario and need help in resolving a tricky situation in the scenario
There is an Xcode project and am using EzSwipeController for swipe (pagination effect) between three View Controllers at the moment.
In my first ViewController (this viewController is fetched from my custom dynamic framework as part of my requirement) -
Code to fetch ViewController:
userProfile.createProfileUI(userSession!) { result in
switch result {
case let .Success(profileViewController):
myDetailsVC = profileViewController //myDetailsVc is passed to EZSwipControllerDataSource array
default:
break
}
}
The other two ViewControllers are within my project storyboard
The Problem -
In the first ViewController, there is a tableView with canEditRowAtIndexPath enabled for few cells (phone numbers).
So when I try to swipe the row, the EZSwipeController responds first and
hence, I am not able to edit the row.
Here is what is happening - http://recordit.co/SOJgdeYchP
Here is what should happen - http://recordit.co/EBPSbjH31q
How do I handle this problem? Is there a way where I can override the default swipe controller action when I try to edit the row?
Please help!
Attach the swipe gesture recognizer to a parent in the hierarchy.
If you're using a UITableViewController, replace it with a UIViewController with a UITableView inside it. Then just drag the gesture recognizer onto the view controller in Storyboard, and it'll attach to the UIViewController's Content View instead of the UITableView.
Though at the end of the day, this is inherently a flawed approach, since swiping to flip pages in an app is only ever viable if you don't have any elements on the pages that also have swipe gestures in them. If you do, even if you code a workaround to make the gesture recognizer for the element in the view controller (in this case, a table view cell) fire instead of the page flipping swipe gesture, that creates an inconsistent user experience.
My suggestion: don't use a table view for such a form altogether. On top of the aforementioned mechanical UX issue, from a user perception perspective, there's nothing indicating visually that this is a table view and not just a scroll view, so there's nothing indicating to the user that swipe actions (Delete) are available. Use a scroll view instead, and take a different approach to deleting (Delete buttons that are always visible, Delete buttons that are only visible after the user hits Edit somewhere, etc.).

Drag & Drop in MAster-DetailView Controller in iOS

I want to drag from the masterview tableview Cell to DetailView.
I try with touchesBegan & touchesEnded method but not working.
Can you please help me for this?
Thank you
This is actually not easy. You can start by
Adding a pan gesture recognizer (UIPanGestureRecognizer) to the root view controller's view (UIWindow.keyWindow!.rootViewController!.view).
When the pan starts (i.e. user touches the screen), loop through the master view's table view's visible cells to see if the point is inside any cell by using UIView's convertPoint:fromView:. You may need to adjust timing to avoid interfering with table view's scrolling and tapping.
If a cell contains the pan's point, create an "indicator view" (that shows that user is dragging) and add it to the root view controller's view, on top of everything else and position it properly, e.g. under user's finger.
When the pan changes (i.e. user moves his finger), update the indicator view's location.
When the pan ends (i.e. user releases his finger), check if the point is inside the detail view and do whatever you need to do.
Check this out. It demonstrates how to do drag drop within a view. Your problem is more complicated as it involves different view controllers, hence the touch handling must be done at a level higher than both master and detail view controllers.
Why are you using touchesBegan & touchesEnded methods? If you have the tableView you should use didSelectRowAtIndexPath delegate method. And you can also use segues if you are using storyboards.

How to make UITableView header to show only when dragging down table view

I have an UILabel that shows me the current date added to the table view header. When the app is running you can see the header right from the start.
What I am after is when the app starts to not see the header, only when you try to drag the table view down, to reveal the label. And when releasing the header should be hidden again.
I think Snapchat uses this technique in their Stories screen.
Any ideas how can this be achieved ?
I'm thinking something with setContentOffset when the app starts to move the table view on top of the header. But then how do I do I know when the table view was dragged and released ?
You are correct that you can use setContentOffset to move the tableViewDown. You can implement UIScrollViewDelegate method scrollViewWillEndDragging(:withVelocity:targetContentOffset:) which will tell you when the user lets go of the tableView and then call setContentOffset(:animated:) to animate the tableView back to its initial position.

Can't touch UITextField after scrolling

I have a ViewController setup like so:
-View
--scrollView
---contentView (UIView)
All of my content is in the contentView. Most of the content is UITextFields that I added programmatically to fashion a form-like setup. I can touch the text fields when I first enter the View Controller to bring up the keyboard to type on them, however, as soon as I scroll down (so I can see the rest of the textfields on the view) none of them respond to touch, even the ones that I could originally see and interact with when I first entered the view controller. There is, however, one button that I added from the storyboard that will continuously accept touch events even after scrolling, but the textfields I added programmatically will not.
I am not sure what code to place here and I don't have the first clue as to where to look for this sort of situation. Any help would be awesome, this is my first app so things are coming along slowly. Thanks so much in advance.
Code Here

In iOS 8 what is the correct way to add a gesture recognizer on everything in my view controller except a button, without adding an additional button?

I have a button that when tapped shows the button text. In iOS 8 what is the correct way to implement a gesture recognizer to my view controller, so that when anything else is tapped besides that button will remove that text. I would like to avoid adding an additional button because I have multiple buttons that I would like to apply this gesture recognizer to in addition.
You could try using touchesBegan - that would capture touches elsewhere, however if you have another UI element in the view that interacts with touch I believe touchesBegan will not be called in that case as the UI element will not pass the touch event on.
Implement it and pop some logging in there and see what's going on. Could be the solution you're after.

Resources