UITableViewCell animate height issue in iOS 10

后端 未结 7 1861
余生分开走
余生分开走 2020-12-13 14:23

My UITableViewCell will animate it\'s height when recognizing a tap. In iOS 9 and below this animation is smooth and works without issues. In iOS 10 beta there\

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 15:11

    So I had this issue for a UITableViewCell that used layout constraints and a UITableView that used automatic cell heights (UITableViewAutomaticDimension). So I'm not sure how much of this solution will work for you but I'm putting it here as it's closely related.

    The main realisation was to lower the priority of the constraints that cause the height change to happen:

    self.collapsedConstraint = self.expandingContainer.topAnchor.constraintEqualToAnchor(self.bottomAnchor).priority(UILayoutPriorityDefaultLow)
    self.expandedConstraint = self.expandingContainer.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor).priority(UILayoutPriorityDefaultLow)
    
    self.expandedConstraint?.active = self.expanded
    self.collapsedConstraint?.active = !self.expanded
    

    Secondly, the way in which I animate the tableview was very specific:

    UIView.animateWithDuration(0.3) {
    
        let tableViewCell = tableView.cellForRowAtIndexPath(viewModel.index) as! MyTableViewCell
        tableViewCell.toggleExpandedConstraints()
        tableView.beginUpdates()
        tableView.endUpdates()
        tableViewCell.layoutIfNeeded()
    }
    

    Note that the layoutIfNeeded() had to come AFTER beginUpdates/endUpdates

    I think this last part might be helpful to you....

提交回复
热议问题