I have a small UITableView that is hidden when the view is loaded. When i click on "SHOW" UIButton, the UITableView is made visible by myTableView.hidden=NO;
I want to hide UITableView when a user touches outside its frame. Thanks for any help!
I have a small UITableView that is hidden when the view is loaded. When i click on "SHOW" UIButton, the UITableView is made visible by myTableView.hidden=NO;
I want to hide UITableView when a user touches outside its frame. Thanks for any help!
Best Approach
Simple.Before show up the UITable View add one more grayed out/Transparent view then add tap gesture recogniser on it to hide it . That's it.
Show Overlay View first - alpha will be 0.5f and background color should be clear color.
show the table view.
NOTE: over lay view should have tap recogniser which will hide the overlay and table view
in View did load
Bad Approach
We should not add tap recogniser on main view itself. Because it may have lots of controls inside of it. so when user tap on it. it will perform its operation. So to avoid it we can simulate the same behaviour by above approach
Subclass
UITableView
, overridepointInside:withEvent:
. It is templated for this reason.Something like:
So you can catch the touch in table view implementation, where it belongs logically in this case.
You can get touch position by this:
Then just check if the point is in tableview frame. If not then hide the
tableview
. Hope this help. :)