3

I want to increase the height of UITableView when user scroll to top and decrease height when user scroll to down. I have done this situation by this code

if scrollView == tableView {
     print(scrollView.contentOffset.y)
     let height: CGFloat = scrollView.contentOffset.y+200
     let maxHeight: CGFloat = self.view.bounds.size.height - 64
     let minHeight:CGFloat = 200
     if height < maxHeight && height > minHeight {
         UIView.animateWithDuration(0.25, animations: {() -> Void in
            self.tblHeightCons.constant = height
            self.view.setNeedsUpdateConstraints()
          })
      }

 }

https://drive.google.com/file/d/0B9WA3RAMmfrKQlBVOG9NaEllRTQ/view

But i don't want to move contents..

3 Answers 3

2

Updated. It implements the effect like in videos, and disable the content moving when scrolling in range, and enable the content moving when scrolling out of range. The fast scrolling problem is also fixed:

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let maxHeight: CGFloat = self.view.bounds.size.height - 64
    let minHeight:CGFloat = 200
    var height = self.tblHeightCons.constant + scrollView.contentOffset.y

    if height > maxHeight {
        height = maxHeight
    }
    else if height < minHeight {
        height = minHeight
    }
    else{
        scrollView.contentOffset = CGPoint(x: 0, y: 0)
    }

    self.tblHeightCons.constant = height
}
Sign up to request clarification or add additional context in comments.

4 Comments

When we do scrolling fast its fail to reach out to a top.
@SalmanGhumsani, code is updated to fix "reach out" problem. Please try again.
@YunCHEN I've tried your code on a similar case i have. Once it scroll to max height, it doesn't decrease table view height
@ItaiSpector, did you bind the TableView's height constraints to the variable we change in function? Please post a new question to provide more details and leave a link here If the situation is too complicated.
0

The best way i have used following way to achieve this, set your dynamic height to tableview and set contentSize for scrollview in the tableView:numberOfRowsInSection: method.

_commentsTable.frame = CGRectMake(self.commentsTable.frame.origin.x, self.commentsTable.frame.origin.y, self.commentsTable.frame.size.width,cellDynamicHeight*_commentArray.count) ;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height+kConstantHeight);

2 Comments

Please watch the video that was uploaded.
@SalmanGhumsani if you want to do something like this link use parallaxheader (cocoacontrols.com/controls/vgparallaxheader)
0

This will automatically handle scrollview and tableview in accordance with tableview height.

  @IBOutlet weak var heightConstraint: NSLayoutConstraint!// tableView height constraint 
     func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
                self.heightConstraint.constant = tableView.contentSize.height
            }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.