首页 > 解决方案 > Swift4滚动UITableview后,动画UITableview框架是错误的

问题描述

testView 是 subview 有 testView 和 view NSLayout

self.view.addSubview(testView)
testView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: testView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: testView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0).isActive = true
self.testWidth = NSLayoutConstraint(item: testView, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1.0, constant: 0.0)
self.testWidth.isActive = true
self.testHeight = NSLayoutConstraint(item: testView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1.0, constant: 0.0)
self.testHeight.isActive=true

tblview 是 UITableview tblview 是 testView 的子视图 tblview 和 testView NSLayout

self.testView.addSubview(self.tblView)
self.tblView.translatesAutoresizingMaskIntoConstraints=false
self.tblView.register(NotYetWriteCell.self, forCellReuseIdentifier: "NotYetWriteCellPage")
NSLayoutConstraint(item: tblView, attribute: .centerY, relatedBy: .equal, toItem: self.testView, attribute: .centerY, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: tblView, attribute: .centerX, relatedBy: .equal, toItem: self.testView, attribute: .centerX, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: tblView, attribute: .width, relatedBy: .equal, toItem: self.testView, attribute: .width, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: tblView, attribute: .height, relatedBy: .equal, toItem: self.testView, attribute: .height, multiplier: 1.0, constant: 0.0).isActive = true

UITableview 委托,数据源

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 50
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print("cell")
    let cell = tableView.dequeueReusableCell(withIdentifier: "NotYetWriteCellPage", for: indexPath) as! NotYetWriteCell
    
    cell.contentView.layoutIfNeeded()
                   
    
    return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  
    print(self.tblView.frame.height)
    return self.testView.frame.height/7
    
}

动画动作代码

self.testWidth.isActive = false
self.testHeight.isActive = false

self.testHeight = NSLayoutConstraint(item: testView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.5, constant: 0.0)
self.testHeight.isActive = true
self.testWidth = NSLayoutConstraint(item: testView, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.5, constant: 0.0)
self.testWidth.isActive = true
UIView.transition(with: self.view, duration: 2.0, options: .curveEaseIn, animations: {
    self.testView.layoutIfNeeded()
}, completion: { (finish) in
    if finish{
        self.took=true
    }
        
})

标签: swiftuitableviewanimationnslayoutconstraint

解决方案


我的自定义单元格这个。

class NotYetWriteCell: UITableViewCell {
let mainImage=UIView()
let mainLabel=UILabel()
var animation:NSLayoutConstraint!

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    mainLabel.text="tableCell"
    //mainLabel.textAlignment = .center
    mainImage.addSubview(mainLabel)
    mainLabel.translatesAutoresizingMaskIntoConstraints = false
    //self.mainLabel.numberOfLines = 0
    
    NSLayoutConstraint(item: mainLabel, attribute: .width, relatedBy: .equal, toItem: mainImage, attribute: .width, multiplier: 1.0, constant: 0.0).isActive=true
    
    NSLayoutConstraint(item: mainLabel, attribute: .centerX, relatedBy: .equal, toItem: mainImage, attribute: .centerX, multiplier: 1.0, constant: 0.0).isActive=true
    animation = NSLayoutConstraint(item: mainLabel, attribute: .centerY, relatedBy: .equal, toItem: mainImage, attribute: .centerY, multiplier: 1.0, constant: 0.0)
    animation.isActive = true
    
    mainImage.backgroundColor = .groupTableViewBackground
    self.contentView.addSubview(mainImage)
    mainImage.translatesAutoresizingMaskIntoConstraints=false
    mainImage.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0.0).isActive=true
    mainImage.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0.0).isActive=true
    mainImage.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -10.0).isActive=true
    mainImage.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10.0).isActive=true
    self.selectionStyle = .none
    contentView.backgroundColor = .groupTableViewBackground
}


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}


推荐阅读