1

I want to have a uiview as cell background with changing width. This should be used as a progress-/ statusbar.

The problem:

I can’t change the width of the statusbar from my collectionViewController (only from the CollectionViewCellController)....

I manually set the width to 5 in layoutSubviews(). Then I changed it to 80 in setStatusBar(...) which I called in the other class. The width stays at 5 :(

Here is my code

import UIKit

class MyCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var myLabel: UILabel!
    @IBOutlet weak var statusbar: UIView!
    
    override func layoutSubviews() {
        // cell rounded section
        self.layer.cornerRadius = 15.0
        self.layer.masksToBounds = true
        super.layoutSubviews()
        statusbar.frame = CGRect(x: 0, y: 0, width: 5, height: self.frame.height)
    }
}

extension MyCollectionViewCell{
    
    func setStatusbar(completedTasks: Int, totalTasks: Int){
        print(self.frame.width)
        let newWidth = 80 //(self.frame.width * (CGFloat(completedTasks / totalTasks)))
        statusbar.backgroundColor = .orange
        statusbar.frame = CGRect(x: 0, y: 0, width: newWidth, height: Int(self.frame.height))
        reloadInputViews()
    }
}

And this is where I call the function setStatusbar(...) - where I want to change the width, but DOESNT work:

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
        
        // Use the outlet in our custom class to get a reference to the UILabel in the cell
        cell.myLabel.text = self.items[indexPath.row] // The row value is the same as the index of the desired text within the array.
        cell.backgroundColor = UIColor(rgb: 0xF444444)
        cell.setStatusbar(completedTasks: 5, totalTasks: 25) //!!! HERE !!!
        return cell
    }

Can someone help me find the problem why the statusbar uiview doesn’t change its with from 5 to 80?

Thank you!

MunichCoder
  • 143
  • 1
  • 9

0 Answers0