UITableViewCell animate height issue in iOS 10

后端 未结 7 1857
余生分开走
余生分开走 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:23

    The solution for iOS 10 in Swift with AutoLayout :

    Put this code in your custom UITableViewCell

    override func layoutSubviews() {
        super.layoutSubviews()
    
        if #available(iOS 10, *) {
            UIView.animateWithDuration(0.3) { self.contentView.layoutIfNeeded() }
        }
    }
    

    In Objective-C:

    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        NSOperatingSystemVersion ios10 = (NSOperatingSystemVersion){10, 0, 0};
        if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios10]) {
            [UIView animateWithDuration:0.3
                             animations:^{
                                 [self.contentView layoutIfNeeded];
                             }];
        }
    }
    

    This will animate UITableViewCell change height if you have configured your UITableViewDelegate like below :

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return selectedIndexes.contains(indexPath.row) ? Constants.expandedTableViewCellHeight : Constants.collapsedTableViewCellHeight
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        tableView.beginUpdates()
        if let index = selectedIndexes.indexOf(indexPath.row) {
            selectedIndexes.removeAtIndex(index)
        } else {
            selectedIndexes.append(indexPath.row)
        }
        tableView.endUpdates()
    }
    
    0 讨论(0)
提交回复
热议问题