I need to have a UIPageViewController to make a slideshow of my images at the top of the screen ...i think the good example is Airbnb app that when you choose a house you will be pushed to view controller that at top contains array of images in a PageViewController (i think)
i have to say i have an app that is powered by collectionView and i want this to be on my top UICollectionViewCell...
also i found someThing about using containers but i don know how to add that...
and someThing else is I DONT use StoryBoard ...
import UIKit
class ImageCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupView() }
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let imageView : UIImageView = {
let image = UIImageView()
image.image = #imageLiteral(resourceName: "Apple_Watch_Main")
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()
func setupView() {
backgroundColor = .black
addSubview(imageView)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : imageView]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : imageView]))
}
}
this is the cell that i want to add that UIPageViewController
I DID FOUND THE ANSWER
i made a calss like this
class PageViewController: UIPageViewController , UIPageViewControllerDataSource{
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
let frameViewController = FrameViewController()
frameViewController.imageName = imageNames.first
let viewControllers = [frameViewController]
setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)
}
let imageNames = ["Apple_Watch_Main" , "Pic2" , "Pic3"]
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentImageName = (viewController as! FrameViewController).imageName
let currentIndex = imageNames.index(of: currentImageName!)
if currentIndex! > 0 {
let frameViewController = FrameViewController()
frameViewController.imageName = imageNames[currentIndex! - 1]
return frameViewController
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentImageName = (viewController as! FrameViewController).imageName
let currentIndex = imageNames.index(of: currentImageName!)
if currentIndex! < imageNames.count - 1 {
let frameViewController = FrameViewController()
frameViewController.imageName = imageNames[currentIndex! + 1]
return frameViewController
}
return nil
}
}
and in my collection View i made my first cell fully empty so it was like a window then i added the child view controller and the view of page controller to that cell now it works great
here is that code
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch indexPath.item {
case 0 :
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as!ImageCell
let pagecontroller = PageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
self.addChildViewController(pagecontroller)
cell.addSubview(pagecontroller.view)
pagecontroller.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/3)
return cell
case 1 :
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "filterandorganize", for: indexPath) as! FilterAndOrganize
return cell
case 2 :
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Offers", for: indexPath) as! Offers
return cell
case 3 :
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OfferedProducts", for: indexPath) as! OfferedProducts
cell.firstViewController = self
return cell
default:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Offers", for: indexPath) as! Offers
return cell
}
}
you see in case 0 (my first cell) i added it
and it works great
I wrote Answer as an Edit And it works great