0

iOS newbie here. I'm using collection view controller with many cells and some of the cells on the bottom are cut off (not being displayed) as I scroll to the bottom.

I tried implementing UICollectionViewDelegateFlowLayout protocol. I also tried setting self.view.frame.size.height. However, I can only set the self.view.frame.size.height to be something smaller like 200 but when I try to go above the default size, the screen doesn't get longer.

Is there a default constraint that I'm missing? Also would autolayout solve the problem?

Thanks!

EDIT:

I tried to add contraints - like doing self.view.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 8).isActive = true but not sure what are the parameter values should be for "equalTo". My goal is to add contraints to the main view in the view controller.

here is my code - thanks in advance!

class ViewController: UICollectionViewController   {


override func viewDidLoad() {
    super.viewDidLoad()
    // self.clearsSelectionOnViewWillAppear = false


    self.view.frame.size.height = 1500
    // Register cell classes
    self.myCollectionView!.register(TableCell.self, forCellWithReuseIdentifier: "tableCell")



}




override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}



func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: 200)
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return 8
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "tableCell", for: indexPath) as! TableCell
    cell.contentView.backgroundColor = UIColor.blue

    return cell
}

}

class TableCell : UICollectionViewCell {

override init(frame: CGRect) {
    super.init(frame: frame)

}
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}
vaibhav
  • 4,038
  • 1
  • 21
  • 51
xcalysta
  • 59
  • 3
  • 10
  • check constraints for collection view.Give your collection view leading,trailing,top,bottom constraints if its covering complete controller height and width.If you have tab bar or navigation bar embedded as well manage frame according to that and give same constraints. – Tushar Sharma Jul 24 '17 at 04:22
  • how do I manage to add constraints with the nav bar on top? – xcalysta Jul 24 '17 at 04:33
  • you giving constraints from storyboard or programmatically? – Tushar Sharma Jul 24 '17 at 04:34
  • programmatically – xcalysta Jul 24 '17 at 04:37
  • navigation bar height is around 64 i guess. You can use anchor constraints method and provide top constant as 64 and rest goes same. – Tushar Sharma Jul 24 '17 at 04:38
  • 1
    Show. Your. Code. You don't use constraints on a collection view's cells so it sounds like you're doing something very wrong. Explain how the collection view gets into the interface. You are giving NO information that would allow anyone to help you. – matt Jul 24 '17 at 04:41
  • @xcalysta Check this if it helps https://stackoverflow.com/a/42669920/4715546 – Parth Adroja Jul 24 '17 at 05:28
  • thanks! @ParthAdroja - I looked at that already - is there a way to do it progrmattically i don't want to mix story board and not using storyboard. – xcalysta Jul 24 '17 at 05:45
  • you can't set self.view.frame.size.height = 1500 its read only – Sunil M. Jul 24 '17 at 06:27

2 Answers2

0

using following lines of codes you can give constraints programmatically if you want

containerView.addSubview(inputTextField)
        //x,y,w,h
        inputTextField.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 8).isActive = true
        inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
        inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true

and if you use storyboard for constraints yes it will solve your problem [Easy way]

iOS Geek
  • 4,825
  • 1
  • 9
  • 30
0

Try this code-:

      class ViewController: UIVIewController,UICollectionViewDataSource,UICollectionViewDelegate   {


        override func viewDidLoad() {
            super.viewDidLoad()
            // Register cell classes
            setUPView()
   }

        // CREATE COLLECTION VIEW

            lazy var collectionView : UICollectionView = {

            let layout = UICollectionViewFlowLayout()
            var collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
            collectionView.delegate = self
            collectionView.datasource = self
            collectionView.backgroundColor = UIColor.yellow
            return collectionView

            }()

            func setUPView(){
        // ADD COLLECTION VIEW ON VIEW
            view.addSubview(collectionView)
        collectionView.register(TableCell.self, forCellWithReuseIdentifier: "tableCell")
        // SET UP CONSTRAINTS
            collectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
            collectionView.topYAnchor.constraint(equalTo:view.topAnchor,constant:64).isActive = true
                collectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
                collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true


        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }



       func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1
        }



        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: view.frame.width, height: 200)
        }

       func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            // #warning Incomplete implementation, return the number of items
            return 8
        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "tableCell", for: indexPath) as! TableCell
            cell.contentView.backgroundColor = UIColor.blue

            return cell
        }

     }
Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38