2

I have a UITableView in the bottom of UIViewController and tableview height is 100 point now. storyboard

The tableview has 20 cells and tableview's header view is 100 point. And I've added a up UISwipeGestureRecognizer and a down UISwipeGestureRecognizer in table view header.

Now I want to change the tableview height constraint constant to 400 in up gesture action and change the tableview height constraint constant to 100 in down gesture action.

minimized

maximized

  • Now the problem is gesture recognizer isnt working in tableview header when tableview scroll is enabled.
  • If tableview scroll is disabled then the gesture recognizer is working. But unable to view all cells once the tableview height is changed.
RajeshKumar R
  • 13,989
  • 2
  • 35
  • 63

4 Answers4

5

Here's a different approach. Don't use swipe gesture recognizers at all.

Instead, make the table always the full 400 points tall. Set its contentInset.top to 300. This will allow the table view to scroll so that only its top 100 points of content are visible at the bottom of the screen. Specifically, the table view will allow its contentOffset.y (which is its vertical scrolling position) to go down to -300 (instead of only down to 0). The table's content always starts at y = 0, so when the table's contentOffset.y is -300, only the top 100 points of its content are visible. As the contentOffset.y increases, more of its content becomes visible.

Then, override the table view's point(inside:withEvent:) method to return true only for points with y >= 0. This means the table will ignore (pass through) touches above its content when its content is scrolled so only the top 100 points are visible.

This is the final effect for a small table:

small table

or for a big table:

big table

You can find a detailed explanation (in Objective-C) and a link to the full test project (also in Objective-C) in this answer.

rob mayoff
  • 342,380
  • 53
  • 730
  • 766
  • 1
    When you want it hidden, set its contentInset.top ................. brilliant!!!!!!!!! – Fattie Jun 16 '17 at 16:34
  • Set the `contentInset.top` **always**. The `contentOffset.y` (which is to say, its vertical scrolling position) determines whether it's hidden. I've updated my answer to be more clear about that. – rob mayoff Jun 16 '17 at 18:25
3

As it may help others, here is the simplest possible solution to this type of problem.


Extremely simple solution -

no tricks, nothing fancy -

don't have a "table view header".

Just make a new UIViewController

class TableWithRedTop: UIViewController

that has ...

[ red area .. swipe detection ]
[ main area - the table view]

Then simply put 'TableWithRedTop' inside a container view.

(Container view tutorial if you need one.)

In your MainView just have a call

func toggleTableHeight

When 'TableWithRedTop' gets a swipe, have one line of code to call toggleTableHeight in MainView

Note that in toggleTableHeight you can easily animate the height change, tilt it on an angle, duplicate it or do anything you want, as you're using a container view in MainView.

Fattie
  • 30,632
  • 54
  • 336
  • 607
  • Thanks for the answer. I've tried with a container view with a UIView(redview), UITableview. But the redview is floating in top of container view when i scroll the tableview. – RajeshKumar R Jun 14 '17 at 12:20
  • Ah, you want it to **"go away with"* the table view. That's almost impossible. – Fattie Jun 16 '17 at 15:05
2

I'm going to make another suggestion that may solve the issue for you.

It could be what you want is an:

"self-expanding" table...

First implement the following with no animations, for simplicity.

You have two heights for the table, small and large.

Start the table on height small.

Remarkably, you only have to implement these two rules: just two simple lines of code:


Any time the user is scrolling upwards - in fact, change to "height large".

Any time the user is scrolling downwards, and, you are at the top position of the table (i.e. you can see cell #1) in fact change to "height small".


It's one of those things that is "so simple, it's hard to believe it works!"

It's sometimes referred to as a "pull-up table" I think.

(Note. If you're not familiar with detecting when the user is scrolling, fortunately it is trivial - code shown here for example.)

Community
  • 1
  • 1
Fattie
  • 30,632
  • 54
  • 336
  • 607
  • By the way since this is not literally an answer to your excellent question, don't consider it for the bounty! :) – Fattie Jun 23 '17 at 14:03
0

Set both swipe gesture’s cancelsTouchesInView to true and make sure that the gestures are added directly to the header and not the tableView.

That should do the trick, but so should adding a view with the gesture recognizes to the container view and setting it’s constraints to match the tableView’s top, left, and right constraints and setting its height to 100.

Alec O
  • 1,519
  • 16
  • 28