How to animate tableview cells from bottom to top - uitableview

I have a tableview which grows and shrink on user click. When user lands to the screen the height of tableview is same as cell(single cell). On click of that cell multiple cells get populated and tableview height would be same as no. Of cells. I want have animation from last cell to first cell, but what I am getting is animation from first cell to last cell, my tableview is at bottom of the screen(which grows in upper direction)
This is my code inside will display method
cell.alpha = 0
UIView.animate(with
duration:0.4,
delay:0.05*Double
(
indexPath.row),animation: {
cell.alpha = 1
}

According to my understanding, you want a tableView to scroll from bottom to top.
// scroll from bottom to top with delay
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
tableView.setContentOffset(.zero, animated: true)
}

Related

How to make UILabel resize it's dimensions correctly when the view width changes?

I have a UILabel configured to automatically size for multi-lines depending on text length. The label is in a dynamically sizing UITableViewCell with constraints on all size.
The code moves UILabel origin to the right, thereby shrinking the UILabel width.
The label correctly resizes and wraps the text. In the process of positioning the origin back to it's original location, the height of the UILabel expands for 3 lines. But only 2 lines of text are required.
The reason the height expands to 3 lines is that in the process of returning the origin back to the original point with view animation, The text expands to 3 lines and reduces back to 2.
Is there a call or setting to make that will have the cell resize correctly? Or 2 is there a way to keep the cell from resizing when he origin changes. In particular. Is it possible to momentarily disable the UILabel multi-line option as the width shrinks and expands?
Here are screenshots showing the behavior.
Figure (1) Position of label in the UITableViewCell before changing its origin.
Figure (2) Origin of UILabel moves to the right and reduces the width of the label. Notice the second row has 2 lines and the label border tightly hugs the text.
Figure (3) Origin returns the position of the first image. Notice the UIlabel of the second row loosely hugs the text and does not return to it's original height.
#IBAction func editTable(_ sender: UIBarButtonItem) {
if isEditingDynamic {
let animate = UIViewPropertyAnimator(duration: 0.4, curve: .easeIn) {
for cell in self.tableView.visibleCells as! [CellResize] {
cell.lblDescription.frame.origin.x = 16
}
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
animate.addCompletion(){
(position:UIViewAnimatingPosition) in
for cell in self.tableView.visibleCells as! [CellResize] {
cell.leadingContraint.constant = 0
}
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
animate.startAnimation()
isEditingDynamic = false
} else {
let animate = UIViewPropertyAnimator(duration: 0.4, curve: .easeIn) {
for cell in self.tableView.visibleCells as! [CellResize] {
cell.lblDescription.frame.origin.x = 60
}
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
animate.addCompletion(){
(position:UIViewAnimatingPosition) in
for cell in self.tableView.visibleCells as! [CellResize] {
cell.leadingContraint.constant = 54
}
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
animate.startAnimation()
isEditingDynamic = true
}
}
Are you able to post some codes of how your table view cells and controller is set up? Typically, what I will do is I will create a single line UILabel and place it inside the cell's content view, constraining it Top, Left, Trailing & Leading with a height constraint >= current & numberOfLines = 0.
Then, in the tableview cell, I will set its estimatedHeight to be 100 and heightForRow to be UITableViewAutomaticDimension.
In this case, TableView will automatically handle the height of the cell based on the label's contents.

Move constraints of UILabels when swiping a UITableViewCell to the right

I have a custom UITableViewCell composed by two detail labels located at the beginning of the cell (one above the other) a thin view (used as a separator which the labels have their constraints related to), the main label and a button.
I implemented an animation to "hide" (actually moving to a non-visible position) the detail labels after some time when the tableview is loaded.
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.tableView.visibleCells.forEach { (cell) in
let songCell = cell as! SongCell
songCell.layoutIfNeeded()
songCell.leadingHoursSeparatorConstraint.constant = -10
UIView.animate(withDuration: 0.33) {
songCell.layoutIfNeeded()
}
}
}
I want to be able to see the detail labels when swiping the cell to the right. Any suggestions on how to achieve this? Any help will be appreciated :D

Unintended Parallax effect in nested UICollectionView Scrolling in a parent TableView

I have a UITableViewController with embedded collectionView ... Each items of which is either UICollectionView / UITableView.. I need the Parent Tableview to scroll up/down first and only it has completed the child should be scrolled up or down.
I have mostly achieved this but the tableview is not scrolling fully to the bottom (10% remaining) with an unwanted parallax effect while scrolling. I understand this is due to the reason that i am passing the offset from child to parent but i need make the child still so that it only moves with parent and not on its own. But i do not know how to do it.
In the childViewController i did this :
override func scrollViewDidScroll(scrollView: UIScrollView) {
if(parent.tableView.contentOffset.y >= (parent.tableView.contentSize.height - parent.tableView.frame.size.height)) {
//
if self.collectionView?.contentOffset.y <= 0 || self.collectionView?.contentOffset.y >= self.collectionView?.contentSize.height{
print("Child scrolled the top")
parent.tableView?.setContentOffset(scrollView.contentOffset, animated: true)
}
}else {
print("Parent TableView should move")
parent.tableView?.setContentOffset(scrollView.contentOffset, animated: false)
}
This is my Layout
1.Without Scroll 2.While Scrolling Parellax 3.Scrolled(distance remaining in bottom)
The height of HomeTableView = view.frame.height and the same for each item in UICollectionView.
Can anyone help me to get full bottom scroll & remove parallax effect.

How to change the content offset of a UICollectionView simultaneously?

I have a horizontal scrollable collectionView with two cells that take up the screen. Inside those cells is an embedded collection view that scrolls vertically. Is there a way for me to scroll the vertical CV inside the first cell, while using its ContentOffset to scroll the vertical CV in the second cell?
Here's how I'm trying to accomplish it:
let scrollView = notification.userInfo!["scrollView"] as! UICollectionView
if scrollView.contentOffset.y <= 250.0 {
childViewControllerForPosts.collectionViewForGroups?.collectionView.setContentOffset(CGPointMake(0, scrollView.contentOffset.y), animated: false)
self.topVerticalConstraint?.constant = -scrollView.contentOffset.y
}
else {
if self.topVerticalConstraint?.constant > -250 {
// Make sure it stays at -250
self.topVerticalConstraint?.constant = -250
}
}
The FeedCell would be the main collection view (which horizontally scrolls) that contains the vertical scrolling collection view. I'm trying to access the second one to change the vertical contentOffset when I scroll the one in first one.
Here's how I'm trying to save the variable that should contain the second main collection view:
collectionViewForGroups = collectionView(collectionView!, cellForItemAtIndexPath: NSIndexPath(forItem: 1, inSection: 0)) as? FeedCell
EDIT:
Note that when I scroll horizontally, the vertical scroll view didn't change. But I want it to change with the one I just scrolled in the gif. If I were to scroll the 2nd vertical collection view, the UIView above will reappear because the topLayConstraint changes due to the vertical contentOffset change.

UITableView scroll to the top of the cell

I have an UITableViewController with 3 static cells. One with the height of 60.0 at the top and one with the same size at the bottom. The height of the mid cell is dynamic and so big, that all 3 cells together fill the whole screen.
The mid cell contains an UITextView which fills the complete cell.
My problem is that when I want to type something in that TextView the tableview automatically scrolls when the keyboard is rising and I don't see the top of the cell including the cursor anymore until I manually scroll back there.
Is there any way I can prevent the table view from scrolling like this automatically? Or tell it that it should scroll to the top of the cell so I see the cursor?
I've already tried to override the viewWillAppear(_:) method without calling the super method of it but then I can't scroll the tableview manually enough so I can't get to the last cell when the keyboard is visible.
I've also tried to scroll manually to the cell inside the textViewDidBeginEditing(_:) but it changed nothing. My method looked like this.
func textViewDidBeginEditing(textView: UITextView) {
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}
I've no idea what else I could try so I'd appreciate your help.
Well, you can set the contentInset of the tableView like this:
self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 80, right: 0)
So you can use the bottom to compensate the offsets that you want.
mainTableView.scrollsToTop = true
OR
mainTableView.setContentOffset(CGPointZero, animated:true)

Resources