Adjust UIScrollView height based on UITableView

I have a UIScrollView that contains a UIView and a UITableView. My goal is to adjust the height of the UIScrollView to allow me to scroll the contents of the UIScrollView to a specific point.

Here is my view: it has the top of the UIView and UITableView below.

enter image description here

When I scroll, I want the UIView to stop at a specific point, for example: enter image description here

TableView will be able to continue scrolling, but the UIView will be locked until the user scrolls the page and returns the UIView to its original state.

A striking example of what I'm trying to do is AppStore.app on iOS 6. When you view the details of an application, the filter panel for details, reviews, and related moves at the top of the screen and stops. Hope this all made sense.

thanks

+4
source share
6 answers

In the end, I went with a simpler approach. I can’t believe that I have not seen this before. I created two views: one for the UITableView tableHeaderView and one for viewForHeaderInSection. The view that I wanted to remain visible at all times is placed in the viewForHeaderInSection method, and another view is placed in the tableHeaderView property. I think this is a much simpler approach than using scrollview. The only problem I came across with this approach is that all my UIView animations in these two views no longer revive. Here is my code.

[self.tableView setTableHeaderView:self.headerView]; - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return self.tableViewHeader; } 
+2
source

add yourself as a UIScrollViewDelegate to the UITableView and implement - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView so that if your views are in starting positions, they do this:

- your UITableView animates its size in the second state:

  [UIView animateWithDuration:.1f animations:^{ CGRect theFrame = myView.frame; theFrame.size.height += floatOfIncreasedHeight; myView.frame = theFrame; }]; 

- your UIView animates its vertical movement

  [UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^(void){ view.center = CGPointMake(view.center.x , view.center.y + floatOfVerticalMovement); }completion:^(BOOL Finished){ view.center = CGPointMake(view.center.x , view.center.y - floatOfVerticalMovement);] 

Finally, we always implement – scrollViewDidScrollToTop: in order to let you know that you can return to its original state (using the same methods that were canceled).

UPDATE:

since your views are inside the scroll, there is an easier way if you are fine with a table view that is partially outside your starting position (i.e. instead of resizing it, it just scrolls):

size the scroll frame to the final tableview + size of your initial (total) view and place it at 0.0 (so that its final part will be hidden outside the screen)

 scrollview.frame = CGRectMake(0,0,tableview.frame.size.width,tableview.frame.size.height + view.frame.size.height); 

you make the contents of the scrollview container as big as the whole table view + the whole view + view size you want when scrolling the table view.

 scrollview.contentSize = CGSizeMake(scrollview.frame.size.width, tableview.frame.size.height + view.frame.size.height + floatOfViewHeightIWantOutOfTheWay); 

you put the view one by one in the scrollview, leaving all the extra white space after the table view

 view.frame = CGRectMake(0,0,view.frame.size.width, view.frame.size.height); tableview.frame = CGRectMake(0,view.frame.size.height, tableview.frame.size.width, tableview.frame.size.height); 

Now it should just work, because it supports iOS 3 nested scrolling

+1
source

You can easily achieve this by properly adjusting the size of the scrollView content and keeping the height of the UITableView smaller than the height of your viewcontroller so that it matches the bottom of the top UIView and UITableView ...

Another scenario is to split the top view into 2 parts. The part that will scroll, and the part that will be visible.

Then set the part that will scroll as the entire UITableView title and the part that will remain visible as the title for the first section of the table. That way you can achieve this with a single UITableView without using a UIScrollView

0
source

What you are looking for is similar to what Game Center makes with it a title that can really be modeled with a table title, a custom section title header and some very clever calculations that are not really related to a frame with a frame and table borders .

First, the easy part: a mock sticky presentation. This is the "view that is always present when scrolling through the table", implemented as the section heading. By making the number of sections in table 1 and implementing -headerViewForSection: you can easily scroll the view using tableview all for free (via API):

 - (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,50)]; label.text = @"Info that was always present when scrolling the UITableView"; label.textAlignment = UITextAlignmentCenter; label.backgroundColor = [UIColor colorWithRed:0.243 green:0.250 blue:0.253 alpha:1.000]; label.textColor = UIColor.whiteColor; return label; } 

Finally, the tricky part: KVO. When the table scrolls, we need to keep the title there sticky relative to the top of the view frame, which means you can KVO contentOffset , and use the resulting change in value to get closer to the frame, which should follow the view with little magic MIN() . Assuming your title is 44 pixels high, the code below calculates the corresponding frame value:

 CGPoint offset = [contentOffsetChange CGPointValue]; [self.tableView layoutSubviews]; self.tableView.tableHeaderView.frame = CGRectMake(0,MIN(0,offset.y),CGRectGetWidth(self.scrollView.frame),44); 

If the above is not possible, SMHeadedList really has a rather large and little-known example of how difficult it is to implement a "double table". This implementation has the added benefit of allowing you to view the "header" table view using the "main" table view.

For future visitors, I implemented a much simpler version , although the one that achieves the goal with Reactive Cocoa, and a slightly different result. However, I believe this may be relevant.

0
source

What if you split UIView into top and bottom. At the bottom there will be information.
Set UITableView.tableHeaderView = topView to viewDidLoad
and return bottomView as the section title in the delegate method to make it float:

 (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section { return bottomView; } 
0
source

Just using a UITableView can solve your problem. there is no need to use another type of scrolling.

  • set the view as a UITableView title. Then add the current view to the header view.
  • complete - (void)scrollViewDidScroll:(UIScrollView *)scrollView; . Tn is the function of checking the contents on the scroll screen and set the current view frame.
0
source

All Articles