4

I m trying to design one screen which contain swipe part. I have one screen with one imageview and at the bottom there are two buttons. I want to swipe only the imageview not the bottom area. Screen is like this

enter image description here

At the moment the problem it is swiping the whole screen including buttons too.

Here is my code

ViewController.swift

import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource {

    var pageImages:NSArray!
    var pageViewController:UIPageViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        pageImages = NSArray(objects: "startscreen1","startscreen2","startscreen3")


        self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("MyPageViewController") as! UIPageViewController

        self.pageViewController.dataSource = self

        let initialContenViewController = self.pageTutorialAtIndex(0) as TutorialScreenViewController

        let viewControllers = NSArray(object: initialContenViewController)

        self.pageViewController.setViewControllers(viewControllers as! [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)





       /* self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)*/

        self.pageViewController.view.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height-100)

        self.addChildViewController(self.pageViewController)
        self.view.addSubview(self.pageViewController.view)
        self.pageViewController.didMoveToParentViewController(self)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func pageTutorialAtIndex(index: Int) -> TutorialScreenViewController
    {

        let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("TutorialScreenViewController") as! TutorialScreenViewController

        pageContentViewController.imageFileName = pageImages[index] as! String
        pageContentViewController.pageIndex = index


        return pageContentViewController

    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
    {

        let viewController = viewController as! TutorialScreenViewController
        var index = viewController.pageIndex as Int

        if(index == 0 || index == NSNotFound)
        {
            return nil
        }

        index--

        return self.pageTutorialAtIndex(index)

    }

     func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?

     {
        let viewController = viewController as! TutorialScreenViewController
        var index = viewController.pageIndex as Int

        if((index == NSNotFound))
        {
            return nil
        }

        index++

        if(index == pageImages.count)
        {
            return nil
        }

        return self.pageTutorialAtIndex(index)
    }



    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int

    {

    return pageImages.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int // The selected item reflected in the page indicator.

    {
    return 0

    }


}

TutorialScreenViewController.swift

@IBOutlet weak var myImageView: UIImageView!
    var imageFileName: String!
    var pageIndex:Int!
    var width:CGFloat!

    @IBOutlet weak var loginButton: UIButton!

    @IBOutlet weak var registerButton: UIButton!

      override func viewDidLoad() {
        super.viewDidLoad()

       myImageView.image = UIImage(named:imageFileName)

            }
hellosheikh
  • 2,929
  • 8
  • 49
  • 115

1 Answers1

13

You can use view controller containment:

  1. Create a standard view controller that has the the two buttons and "container" view. Just search for "container" in the object library on the right panel in Interface builder and then drag that onto your main view controller's scene:

    enter image description here

    Obviously, set up your constraints so that this container view is properly laid out with the buttons.

  2. Now control-drag from the container view to your page view controller, and choose the "embed" segue.

You'll end up with a scene that looks like:

enter image description here

Now you can enjoy the page view controller functionality from within a standard view controller with other buttons.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thank you very much. I think you exactly understand what I want but I need your helping implementing your solution as I am new in IOS... okay at the moment my story board look like this https://gyazo.com/58b1d7372b921884f98d0be621968231 now what I understand from your solution is on my first view controller which is empty at the moment which you can also see in my screen shot, to drag a container View right and also put buttons underneath it ? am I right ? If yes then on the tutorialscreencontainer you can see the buttons as well ? should I remove them ? – hellosheikh Dec 07 '15 at 18:06
  • and one last question is when I drag the viewcontainer object it adds another empty view controller on the main story board. hope you understand my question @Rob – hellosheikh Dec 07 '15 at 18:06
  • okay thanks.. one question. in tutorial screen I have added the imageview. now according to your solution should i now add image view on top of containerview which i just dragged – hellosheikh Dec 07 '15 at 18:20
  • okay rob this is my storyboard now looks like https://gyazo.com/a05790644bc8c11dff394aff4207cb75 what I have done first is as I had already an empty view controller in my main story board, I dragged container view and two buttons on that and then I created another view controller and dragged image view on top of that. also I embed the segue between main controller and page view controller. now there is no tutorialscreenview controller anywhere. now I want to know should I now add a class TutorialScreenViewcontroller.swift to my main controller on which there is a container view? @rob – hellosheikh Dec 07 '15 at 18:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97233/discussion-between-rob-and-hellosheikh). – Rob Dec 07 '15 at 20:12
  • FYI, I've shared a demonstration of this (plus added functionality like caching of the child view controller pages, having the page view controller notify the parent when the page changes, etc.). See https://github.com/robertmryan/EmbeddedPageViewController. – Rob Dec 08 '15 at 17:30