0

The code below is my test files for my final project that will look like this. As you can see I will be scrolling by rows of one or two.

I have figured out how to get the horizontal scroll to work, however, it only displays two out of the six buttons as shown here in the simulator. As You can tell, it just repeats itself. I am needing the app to show app six buttons respectively.

The code for this:

var multiButton = [String]()

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    var multiButton = ["faceButton", "faceButtonTwo", "faceButtonThree", "faceButtonFour", "faceButtonFive", "faceButtonSix"]
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return multiButton.count
    }

    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell=collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionViewCell
      //  cell.multiButton.layer.cornerRadius=50.0

        return cell

      //  cell.myWebSeriesImage.image=UIImage(named: webSeriesImages[indexPath.row])
     //   return cell
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

I have a separate file with all my IBOutlets as shown in this code:

import UIKit

class MyCollectionViewCell: UICollectionViewCell {

//  var multiButton = ["faceButton", "faceButtonTwo", "faceButtonThree", "faceButtonFour", "faceButtonFive", "faceButtonSix"]
@IBOutlet weak var faceButtonFour: UIButton!
@IBOutlet weak var faceButtonFive: UIButton!
@IBOutlet weak var faceButtonThree: UIButton!
@IBOutlet weak var faceButtonTwo: UIButton!
@IBOutlet weak var faceButton: UIButton!
@IBOutlet weak var faceButtonSix: UIButton!

I have double checked the Identifiers, classes (class for cells are set for "myCollectionViewCell", which is the same file all IBOutlets are in), made sure all UIButtons were Installed, and have tried to find any abnormalities in the Attributes and Identity Inspector.

I also would like to address the issue of not being able to scroll horizontal without clicking outside the button.

UPDATE for Post:

This is what we are seeing currently after a previous comment to resize the collectionview. It continues to repeat over and over and I am struggling to find the reasons in my code.

This is a screenshot of main.storyboard.

  • Your cellForItemAt method doesn't provide the cell with any data! – Asteroid Jun 03 '22 at 22:29
  • 1
    Also how are you sizing the collectionView? – Asteroid Jun 03 '22 at 22:35
  • @Asteroid I have changed the sizing to 350x350 for the width and height and that seemed to have fixed it! I'm still learning. I am still running into the issue of it repeating over and over though, do you have any ideas in regards to what I should add into the CellForItemAt? As well as not being able to scroll unless hovering outside the button. – Dakota Davis Jun 04 '22 at 04:29
  • I don't see your CollectionView delegate & datasource in viewDidLoad(). also better to move var multiButton = ["faceButton", "faceButtonTwo", "faceButtonThree", "faceButtonFour", "faceButtonFive", "faceButtonSix"] to viewDidLoad() and add yourCollectionViewName.delegate = self & yourCollectionViewName.datasource = self in viewDidLoad() – Mel Jun 04 '22 at 05:31
  • @Mel see the updated picture in the post please. I hate set the delegate and datasource by clicking and dragging the collectionview in the main.storyboard instead of using actual code for simplictity. – Dakota Davis Jun 04 '22 at 05:54
  • you want 6 buttons to show? then just make one button in collection view cell instead of making all and connect in to collectionView class. Your array contains 6 elements so your collection view will repeat that button for 6 times. You don't need to make 6 cells. You can later change colour/label of each button in cellForItemAt function – Mel Jun 04 '22 at 06:26
  • @Mel can you explain in greater detail please. I only have on cell that contains 6 buttons. can you describe in more detail the changes needed here? – Dakota Davis Jun 04 '22 at 06:42
  • it will be hard to explain without giving showing an example. Its better to google about collectionView. For now I can tell you don't put 6 buttons in cell. Put 1 button in cells. A collectionView shows numberOfItems ie.. defined by you. You here are returning number of items = multiButton.count which means 6 cells as count ion array = 6. So collectionView will create 6 cells and your 1 cell contains 6 buttons so 6 *6 = 36 buttons. Create 1 button inside cell and rest is fine. You will get 6 buttons then. – Mel Jun 04 '22 at 06:48
  • can you just tell me what exactly is your requirement? I will simple put answer here according to that and you can make changes accordingly – Mel Jun 04 '22 at 06:49
  • @Mel Sure! I have added an image of my main.storyboard so you can see. I just want buttons (when pressed when go to another screen). The number of buttons may differ by section, but the concept is the same. I will be adding more buttons than what fits on the screen so I need to scroll horizontally to view all the buttons I will be adding. – Dakota Davis Jun 04 '22 at 07:10
  • 1
    if your content is same, you don't make more you just repeat it, that's why we use collectionView. In that screenshot you have added 6 buttons which is wrong. Just add 1 button. You can make any number of buttons based on count of your array. Right now you have added 6 buttons in one cell and "return multi button.count" = 6 which returns 6 cells. So you get 36 buttons. I will add code with comments later – Mel Jun 04 '22 at 07:25
  • @Mel I caught on to what you were saying. Now that I only have 6 buttons, how can I go about editing them individually? (color, text inside, etc?) – Dakota Davis Jun 04 '22 at 08:19
  • I have posted a basic answer regarding all your queries. You can make changes in your code accordingly – Mel Jun 04 '22 at 08:23

1 Answers1

1

This is very basic. You need to create something like this: enter image description here First create a collectionView with required height (50 for example) and add a button inside its cell. Give any width or height to the button (maybe 80 x 30) and add leading, trailing, top and bottom constraints. Make sure to change scroll direction to horizontal as required by you. enter image description here

After that create a collectionView class and connect your button outlet to it:

import UIKit

class ButtonCollectionViewCell: UICollectionViewCell {
    
    @IBOutlet weak var button: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
}

In your main ViewController, connect collectionView and add delegate & datasource function:

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
   
    @IBOutlet weak var collectionViewButtons: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        collectionViewButtons.delegate = self
        collectionViewButtons.dataSource = self
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 6 //number of buttons
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ButtonCollectionViewCell
        
        cell.button.setTitle("Button\(indexPath.item)", for: .normal) //set button title
        
        if indexPath.item == 0 { //first button
            cell.button.backgroundColor = UIColor.purple //set button background
        }
        else if indexPath.item == 1 { //second button
            cell.button.backgroundColor = UIColor.red
        }
        else if indexPath.item == 2 { //3rd button
            cell.button.backgroundColor = UIColor.systemGreen
        }
        else { // for remaining buttons
            cell.button.backgroundColor = UIColor.darkGray
        }
        
        return cell
    }

       func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == 0 { // opens any page by clicking button 1
            let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC1") as! ViewController1
            navigationController?.pushViewController(vc, animated: true)
        }
        else if indexPath.item == 1 {
            let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC2") as! ViewController2
            navigationController?.pushViewController(vc, animated: true)
        }
        else if indexPath.item == 2 {
            let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC3") as! ViewController3
            navigationController?.pushViewController(vc, animated: true)
        }
        else {
            let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC4") as! ViewController4
            navigationController?.pushViewController(vc, animated: true)
        }
    }
    
}

You can return any number of buttons by changing return 6 to any required number or use any array which can further be used to set properties of buttons created.

The didSelectAnItem can be used to add functionality when a cell is tapped.

In the end you should get output like this where you can scroll horizontally to see remaining buttons:

enter image description here

Mel
  • 429
  • 1
  • 4
  • 12
  • Brilliant work! Thank you for that it worked fantastic! Are you aware of how to stack these in rows of two? like this: "123" "456" (this would be directly under the 123). The only piece that did not work was when the button is hovered over, you cannot scroll (ie. you must not have the mouse over the button.) – Dakota Davis Jun 04 '22 at 09:13
  • I don't understand what do you mean by :button is hovered over". And there is better code to adjust collectionView & content size using flow layout but for now just double the height of collectionView. Like in my case button is 30 height and top. bot constraint = 10 so I set collectionView height = 50, (30 + 10 + 10), it will show one row, you can set height = 100 or 110(10 space between cells) and it will show in two rows. This should work in your case for now and is not complicated – Mel Jun 04 '22 at 09:24