作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
出于某种原因,我的 Collection View 代码在启动模拟器时按预期工作,但是当我按下主页按钮关闭应用程序然后重新打开它时,3 列在右侧折叠为一列。我在 Storyboard 中唯一拥有的就是 View 。一切都是通过代码完成的。 Xcode 11.1/Swift 5。
这是第一次打开时的样子:
在重新打开时:
这是所有的代码。
class Online: UIViewController {
weak var collectionView: UICollectionView!
var onlineArray = [[String:AnyObject]]()
override func viewDidLoad() {
super.viewDidLoad()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(collectionView)
NSLayoutConstraint.activate([
self.view.topAnchor.constraint(equalTo: collectionView.topAnchor),
self.view.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor),
self.view.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor),
self.view.trailingAnchor.constraint(equalTo: collectionView.trailingAnchor),
])
self.collectionView = collectionView
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.register(Cell.self, forCellWithReuseIdentifier: Cell.identifier)
self.collectionView.alwaysBounceVertical = true
}
}
extension Online: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier, for: indexPath) as! Cell
cell.textLabel1.text = "A"
cell.textLabel2.text = "B"
cell.textLabel3.text = "C"
cell.textLabel1.backgroundColor = .orange
cell.textLabel2.backgroundColor = .blue
cell.textLabel3.backgroundColor = .green
return cell
}
}
extension Online: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 30)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //.zero
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
class Cell: UICollectionViewCell {
static var identifier: String = "Cell"
weak var textLabel1: UILabel!
weak var textLabel2: UILabel!
weak var textLabel3: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
let width = self.contentView.frame.size.width
let textLabel1 = UILabel(frame: CGRect(x: 10, y: 0, width: 50, height: 20))
let textLabel2 = UILabel(frame: CGRect(x: width/3, y: 0, width: 50, height: 20))
let textLabel3 = UILabel(frame: CGRect(x: (width/3)*2, y: 0, width: 50, height: 20))
textLabel1.translatesAutoresizingMaskIntoConstraints = false
textLabel2.translatesAutoresizingMaskIntoConstraints = false
textLabel3.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(textLabel1)
self.contentView.addSubview(textLabel2)
self.contentView.addSubview(textLabel3)
self.textLabel1 = textLabel1
self.textLabel2 = textLabel2
self.textLabel3 = textLabel3
self.reset()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForReuse() {
super.prepareForReuse()
self.reset()
}
func reset() {
self.textLabel1.textAlignment = .left
self.textLabel2.textAlignment = .center
self.textLabel3.textAlignment = .right
}
}
最佳答案
所以我想我会回答我自己的问题。
translatesAutoresizingMaskIntoConstraints 应该是 true 而不是 false。
textLabel1.translatesAutoresizingMaskIntoConstraints = true
textLabel2.translatesAutoresizingMaskIntoConstraints = true
textLabel3.translatesAutoresizingMaskIntoConstraints = true
关于ios - UICollectionView - 重新打开应用程序时未正确重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58514798/
我是一名优秀的程序员,十分优秀!