Ruhi
Ruhi

Reputation: 137

How to open a ViewController on the cell of CollectionView Controller

like below Image..

enter image description here

I tried the below code...

import UIKit

class UploadsController: UICollectionViewController , Dimmable {

let dimLevel: CGFloat = 0.5
let dimSpeed: Double = 0.5

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    dim(.In, alpha: dimLevel, speed: dimSpeed)
}


@IBAction func unwindFromUploadEdit(segue: UIStoryboardSegue)
{
    dim(.Out, speed:dimSpeed)

}

And connect the AnotherViewController present modally through the button on the cell of the CollectionViewController..

But it does not work.. after click on the button the AnotherViewController open/overlay on the entire CollectionViewController instead of cell of the CollectionViewController...

Plz. suggest me how can I do this I am new in Swift

Upvotes: 0

Views: 40

Answers (1)

Andrew McKinley
Andrew McKinley

Reputation: 1137

Look at the frame of AnotherViewController after it is presented. Compare that to the frame of the cell in the CollectionViewController. You want the width and height to the be the same so that it only covers up that view. The x and y points are a bit more tricky. There is a method on UIView called convertRect:toView: . The origin of the cell is relative to the collection view. The origin of the AnotherViewController should be relative to the entire CollectionViewController's view. Use the mentioned method to determine where the cell is relative to its ViewController and use that information to make sure the overlay is the same size and in the same location of the cell.

Upvotes: 0

Related Questions