1

I have a UIImageView in a .xib file that needs to have its size changed when the user selects a Collection View cell. The .xib file is loaded into the view controller and has an IBOutlet connecting the two. The current method I have worked well with UIView, but fails with UIImageView.

The code is only failing upon the first load of the screen. If the user selects any of the Collection View cells, the UIImageView updates just fine.

func displayDataForCell(index: Int) {
// Set the box fill amount for Anode, Feeder, and Wave
    let anodePercentAmount = Double(pot.anode!)/100
    let feederPercentAmount = Double(pot.feeder!)/100
    let wavePercentAmount = Double(pot.wave!)/100
    dataView.anodeBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)
    dataView.waveBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)
    dataView.feederBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)

    UIView.animateWithDuration(0.5, animations: { () -> Void in
        self.dataView.anodeBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*anodePercentAmount)), width: 120.0, height: (180.0*anodePercentAmount))
        self.dataView.feederBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*feederPercentAmount)), width: 120.0, height: (180.0*feederPercentAmount))
        self.dataView.waveBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*wavePercentAmount)), width: 120.0, height: (180.0*wavePercentAmount))
    })
}

I am calling the above method in the function collectionView(_, didSelectItemAtIndexPath indexPath:_) method as follows:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    // This makes sure only one cell can be selected at once
    if currentPotIndex != indexPath.row {
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell?.highlighted

        let cell0 = collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: currentPotIndex, inSection: 0))
        cell0?.highlighted = false
    }

    self.currentPotIndex = indexPath.row

    displayDataForCell(currentPotIndex)

    bottomCollection.reloadData()

}

Finally, this is my current attempted solution: include the didSelectItemAtIndexPath function call in the viewDidLoad() function.

//Initialize the view with the 0th indexed pot's data
    collectionView(collectionView, didSelectItemAtIndexPath: NSIndexPath(forItem: currentPotIndex, inSection: 0))
    self.dataView.layoutIfNeeded()

I would attach an image but I don't have enough rep. Thanks for looking!

sl0anage
  • 97
  • 10

1 Answers1

0

Here's what I did to address the problem:

Overwrote the function viewDidAppear(animated: Bool) and put the behavior that animated the UIImageViews into this function. I guess this worked because the unconstrained images were being loaded after the function that addressed their size and position in their superview.

sl0anage
  • 97
  • 10