1
現在、カスタムUICollectionViewCell(s)をサブビューとして設定したUICollectionViewがあります。Swift UIcollectionViewcellグリッドレイアウトデバイス回転のサブビューが正しく更新されない(プログラムによって)
これらのカスタムセルの1つをグリッド(6 x 2)にレイアウトしました。デバイス(ランドスケープ)の回転時
、グリッドビューは、正しいフレームの幅が、6X2のレイアウトが失われると膨張します。
は私が
import UIKit
class ActivityCollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
private let cellId = "cell"
// Month names
let months: [String] = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let appsCollectionView: UICollectionView = {
var gridLayout = Gridlayout(numberOfColumns: 6)
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.isScrollEnabled = false
cv.collectionViewLayout = gridLayout
return cv
}()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 12
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
let textLabel = UILabel(frame: .zero)
textLabel.textColor = UIColor.myAppWhite
textLabel.adjustsFontSizeToFitWidth = true
textLabel.textAlignment = .center
textLabel.layer.cornerRadius = 10
textLabel.layer.borderWidth = 1
textLabel.layer.borderColor = UIColor.myAppWhite.cgColor
textLabel.layer.masksToBounds = true
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.text = months[indexPath.row]
cell.contentView.addSubview(textLabel)
cell.addConstraintsWithFormat(format: "H:|[v0]|", views: textLabel)
cell.addConstraintsWithFormat(format: "V:|[v0]|", views: textLabel)
return cell
}
func setupViews() {
self.addSubview(appsCollectionView)
appsCollectionView.delegate = self
appsCollectionView.dataSource = self
appsCollectionView.register(AppCell.self, forCellWithReuseIdentifier: cellId)
addConstraintsWithFormat(format: "H:|[v0]|", views: appsCollectionView)
addConstraintsWithFormat(format: "V:|[v0]|", views: appsCollectionView)
}
// Detetect if UICollectionViewCell was selected
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell : UICollectionViewCell = collectionView.cellForItem(at: indexPath)!
cell.layer.masksToBounds = true
cell.layer.cornerRadius = 10
cell.backgroundColor = UIColor.myAppRed
print("Selected: \(indexPath.row)")
}
// Detetect if UICollectionViewCell was deselected
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell : UICollectionViewCell = collectionView.cellForItem(at: indexPath)!
cell.layer.masksToBounds = true
cell.layer.cornerRadius = 10
cell.backgroundColor = .clear
print("DeSelected: \(indexPath.row)")
}
}
class AppCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
マイカスタムのGridLayoutクラスは、このようになります... ...
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
collectionViewLayout.invalidateLayout()
}
カスタムサブビューを持っているとホームのViewControllerセットアップを持っています...
私のUIView拡張(ビューの制約)
extension UIView {
func addConstraintsWithFormat(format: String, views: UIView...) {
var viewsDictionary = [String: UIView]()
for (index, view) in views.enumerated() {
let key = "v\(index)"
viewsDictionary[key] = view
}
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
}
(×2 6)レイアウト(画面に合わせてのみ、より大きな)同じとどまるように、どのように私は私のGridViewのサイズを変更できますか?
ご意見をいただければ幸いです。