2017-02-16 2 views
1

В настоящее время у меня есть UICollectionView, который имеет несколько настраиваемых настроек UICollectionViewCell (s) в качестве подзонов.Swift UIcollectionViewcell grid layout subView при правильном обновлении устройства (программно)

Я выложил одну из этих пользовательских ячеек в сетку (6 x 2).

Portrait View

При вращении устройства (альбомный), вид сетки расширяет правильная ширина рамы, однако, расположение 6x2 теряется.

Landscape View

Я настроили Главная ViewController с ...

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { 
    collectionViewLayout.invalidateLayout() 
} 

Обычай подвид имеет ...

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 выглядит следующим образом ...

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 


     } 
    } 

} 

расширения Мои 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, так что (6 х 2) расположение остаются теми же (только больше по размеру экрана)?

Любая обратная связь с благодарностью.

ответ

0
override func viewWillLayoutSubviews() { 
     super.viewWillLayoutSubviews() 

     guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else { 
     return 
     } 

     if UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) { 
     //logic for the cell size if phone is in landscape 
flowLayout.itemSize = CGSize(width: 100, height: 100); 
     } else { 
     //logic if not landscape 
flowLayout.itemSize = CGSize(width: 50, height: 50); 
     } 

     flowLayout.invalidateLayout() 
    } 
Смежные вопросы