1
현재 사용자 지정 UICollectionViewCell (s) 설치 수가 하위 뷰로있는 UICollectionView가 있습니다.Swift UIcollectionViewcell 격자 레이아웃 장치 회전에 대한 서브 뷰가 올바르게 업데이트되지 않습니다 (프로그래밍 방식으로)
이러한 사용자 지정 셀 중 하나를 격자 (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 설정을 가지고 ...
import UIKit
class Gridlayout: UICollectionViewFlowLayout {
var numberOfColumns: Int = 6
init(numberOfColumns: Int) {
super.init()
self.numberOfColumns = numberOfColumns
self.minimumLineSpacing = 5
self.minimumInteritemSpacing = 0
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var itemSize: CGSize {
get {
if let collectionView = collectionView {
let columns = 6
let margings = 2 * (columns - 1)
let width = (collectionView.frame.width - CGFloat(margings))/CGFloat(columns)
let height = width // square cells
print ("Hello")
return CGSize(width: width, height: height)
}
//Default
return CGSize(width: 50, height: 50)
}
set {
super.itemSize = newValue
}
}
}
제 (6 × 2) 레이아웃 (화면에 맞게 만 이상) 동일하게 유지하도록
내 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))
}
어떻게 내있는 gridview의 크기를 조정합니까?
모든 의견을 크게 환영합니다.