swift - 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
}
})
解决方案
我的自定义单元格这个。
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")
}
}
推荐阅读
- image - 从我的 api 在我的 xamarin 应用程序中加载图像
- javascript - 按钮按下ajax请求overclick javascript
- javascript - JavaScript isocontouring 算法性能研究
- python - 如何将数字连续添加到子列表中的连接坐标对?
- java - 当我在其他应用程序上时如何更新我的活动位置
- python - 是否可以将破折号数据表导出到磁盘上的特定位置或直接导出到 SQL Server?
- rust - 如何使用从其他两个 HashMap 移动的值创建一个 HashMap?
- javascript - 在 Typescript 中向创建的 Redux Store 添加属性
- bootstrap-4 - 引导程序 4.3.1 下拉列表中的链接不起作用
- routing - 带有自定义 ValueMapper 的 TYPO3 RouteEnhancer