0

I want to set the position of my UICollectionViewCell. After doing some research on it I found a post that explains the process, however in objective-c. I believe I translated it correctly however I can't get it to work.

Here's the link to the question: How do I set the position of a UICollectionViewCell?

Here's the answer in objective-c

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.item == 2) // or whatever specific item you're trying to override
{
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    layoutAttributes.frame = CGRectMake(0,0,100,100); // or whatever...
    return layoutAttributes;
}
else
{
    return [super layoutAttributesForItemAtIndexPath:indexPath];
}
}


- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *layoutAttributes = [super layoutAttributesForElementInRect:rect];
if (CGRectContainsRect(rect, CGRectMake(0, 0, 100, 100))) // replace this CGRectMake with the custom frame of your cell...
{
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    layoutAttributes.frame = CGRectMake(0,0,100,100); // or whatever...
    return [layoutAttributes arrayByAddingObject:layoutAttributes];
}
else
{
    return layoutAttributes;
}
}

Here's my swift version

 func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    if (indexPath.section == 0 && indexPath.item == 1) {

        let layoutAttributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
        layoutAttributes.frame = CGRect(x: 0, y: 0, width: 20, height: 50)
        return layoutAttributes
    }
    else
    {
}
    return layoutAttributesForItem(at:indexPath)
}


func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

    let layoutAttributes = layoutAttributesForElements(in: rect)! as NSArray

    func contains(_ rect2: CGRect) -> Bool {
        if rect2 == CGRect(x: 0, y: 0, width: 2, height: 50) {
            let layoutAttributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            layoutAttributes.frame = CGRect(x: 0, y: 0, width: 20, height: 50) // or whatever...
            return layoutAttributes
        }
        else{
            return layoutAttributes
        }

        }
    }

However in layoutAttributesForElements I get an error saying indexPath is an unresolved identifier...

My VC is subclassed as the following UICollectionViewController, UICollectionViewDelegateFlowLayout, UITextViewDelegate

Any suggestions?

If you know any other methods to reposition UICollectionViewCells that'd be great also.

EDIT

I have included a screenshot.

The red rectangle is the cell. I want it to be on the gray line enter image description here

Stefan
  • 908
  • 1
  • 11
  • 33
  • What do you mean by position my collection view cell? What do you want your cell to behave like? Do you want cells inset within the collection view? Do you want to size your cells a specific way? A little more context would help because your problem isn't easily deduced from your code. – Eli Whittle Jun 03 '17 at 19:45
  • @EliWhittle I know I could use insets but that seems like an improper work around. I want the cell appear at a certain spot in response to a notification. I don't even know if it's possible to animate the cells so I was thinking of just putting it in one spot and changing it's background color to clear. This is because I am going to put a UICollectionView inside of the cell. That's the end goal. UICollectionView inside a UICollectionViewCell that is positioned on the bottom half of the view. Hope that helps. – Stefan Jun 03 '17 at 19:53
  • Do you have a picture of what you're trying to achieve, I'm having a hard time visualizing your problem. – Eli Whittle Jun 03 '17 at 20:51
  • @EliWhittle I have include a screenshot – Stefan Jun 03 '17 at 21:45
  • So you just want to extend your collection view cell down to the grey line when you tap the share button? Or do you want to move the entire collection view down to the grey line (maintaining its size from the image) above the share button? – Eli Whittle Jun 03 '17 at 22:56
  • @EliWhittle I want to move the entire collectionViewCell down to the gray line above the share button – Stefan Jun 03 '17 at 23:00

1 Answers1

2

In order to do this you need to use a UIViewController and put a UICollectionView inside of it.

You can create a var like so:

lazy var collectionView : UICollectionView = {
    layout.scrollDirection = UICollectionViewScrollDirection.horizontal
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.delegate = self
    cv.dataSource = self
    cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: self.cellId)
    cv.backgroundColor = UIColor.red
    return cv
}()

Note: you will have to make your UIViewController conform to UICollectionViewDataSource & UICollectionViewDelegateFlowLayout and include required methods numberOfItemsInSection & cellForItemAt

When you want to move the UICollectionView, you can set it's frame to the new desired position by using:

self.collectionView.frame = <Your_frame>

UICollectionViewController does not allow you to move its collection view instance.

Eli Whittle
  • 1,084
  • 1
  • 15
  • 19
  • Alright. Before I implement this, you are suggesting that I don't use a collectionviewcell from the collectionview I am currently using and insert a collectionview into the view directly and put the collectionviewcell in that? – Stefan Jun 04 '17 at 01:36
  • Don't use a UICollectionviewController. Use a UIViewController and put a UICollectionView in it. It will give you the same behavior as a UICollectionViewController but give you the added benefit of customizing the position of your UICollectionView – Eli Whittle Jun 04 '17 at 18:35