Using UIPageView images to open ViewController w/ NavigationController - ios

I am trying to make three images in a UIPageView to open three different NavigationController. Here is my layout as of right now. I connected three Custom Segue to each NavigationController and under ContentViewController, I added the following #IBAction method:
#IBAction func navJns(sender: AnyObject?){
let storyboard = UIStoryboard(name: "Nav", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Jeans") as! Jeans
self.modalPresentationStyle = UIModalPresentationStyle.Custom
presentViewController(controller, animated: true, completion: nil)
}
#IBAction func navBlusas(sender: AnyObject?){
let storyboard = UIStoryboard(name: "Nav", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Blusas") as! Blusas
self.modalPresentationStyle = UIModalPresentationStyle.Custom
presentViewController(controller, animated: true, completion: nil)
}
#IBAction func navLeggings(sender: AnyObject?){
let storyboard = UIStoryboard(name: "Nav", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Leggings") as! Leggings
self.modalPresentationStyle = UIModalPresentationStyle.Custom
presentViewController(controller, animated: true, completion: nil)
}
And under the viewDidLoad method:
var tapGesturePageView = UITapGestureRecognizer(target: self, action: "navJns:"); self.imageView.addGestureRecognizer(tapGesturePageView)
var tapGesturePageView2 = UITapGestureRecognizer(target: self, action: "navBlusas:"); self.imageView.addGestureRecognizer(tapGesturePageView2)
var tapGesturePageView3 = UITapGestureRecognizer(target: self, action: "navLeggings:"); self.imageView.addGestureRecognizer(tapGesturePageView3)
When I compile and run the app, it shows the three images in the PageView but nothing is happening.
Any advice would be helpful!
Here is the rest of the code:
ContentViewController.swift
import UIKit
class ContentViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
var pageIndex: Int!
var imageFile: String!
override func viewDidLoad()
{
super.viewDidLoad()
self.imageView.image = UIImage(named: self.imageFile)
var tapGesturePageView = UITapGestureRecognizer(target: self, action: "navJns:"); self.imageView.addGestureRecognizer(tapGesturePageView)
var tapGesturePageView2 = UITapGestureRecognizer(target: self, action: "navBlusas:"); self.imageView.addGestureRecognizer(tapGesturePageView2)
var tapGesturePageView3 = UITapGestureRecognizer(target: self, action: "navLeggings:"); self.imageView.addGestureRecognizer(tapGesturePageView3)
}
#IBAction func navJns(sender: AnyObject?){
let storyboard = UIStoryboard(name: "Nav", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Jeans") as! Jeans
self.modalPresentationStyle = UIModalPresentationStyle.Custom
presentViewController(controller, animated: true, completion: nil)
}
#IBAction func navBlusas(sender: AnyObject?){
let storyboard = UIStoryboard(name: "Nav", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Blusas") as! Blusas
self.modalPresentationStyle = UIModalPresentationStyle.Custom
presentViewController(controller, animated: true, completion: nil)
}
#IBAction func navLeggings(sender: AnyObject?){
let storyboard = UIStoryboard(name: "Nav", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Leggings") as! Leggings
self.modalPresentationStyle = UIModalPresentationStyle.Custom
presentViewController(controller, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController, UIPageViewControllerDataSource {
var pageViewController: UIPageViewController!
var pageImages: NSArray!
override func viewDidLoad() {
super.viewDidLoad()
self.pageImages = NSArray(objects: "jeans.png", "blusas.png", "leggings.png")
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
var startVC = self.viewControllerAtIndex(0) as ContentViewController
var viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height - 60)
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 viewControllerAtIndex(index: Int) -> ContentViewController?
{
if index >= self.pageImages.count
{
return nil
}
var vc: ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController
vc.imageFile = self.pageImages[index] as! String
vc.pageIndex = index
return vc
}
// MARK: - Page View Controller Data Source
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
var vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if (index == 0) || (index == NSNotFound)
{
return nil
}
index--
return self.viewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
var vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if (index == NSNotFound)
{
return nil
}
index++
return self.viewControllerAtIndex(index)
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 0
}
}

Related

In PageViewController navigation not working

I have 3 types of VC's , 1st Vc, 2nd VC is PageViewController with 3 pages(I added PageController to normal ViewController here), third one is FinalViewController.
1st VC has button when i click it will move to 2nd VC that is PageViewController(It has 3 pages[3 ViewConrollers] working fine).
In PageViewController after loading first VC, it has view with gestures when i tap that i want to navigate FinalViewController completely.
Here navigation not working.
My 1st VC code
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.navigationBar.isHidden = false
}
#IBAction func btn(_ sender: Any) {
let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "NVC")
self.navigationController?.pushViewController(storyboard!, animated: true)
}
My PageViewController code
import UIKit
class NewViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
#IBOutlet weak var pagerController: UIPageControl!
#IBOutlet weak var pageControllerView: UIView!
// The pages it contains
var pages = [UIViewController]()
// The UIPageViewController
var pageContainer: UIPageViewController!
// Track the current index
var currentIndex: Int?
private var pendingIndex: Int?
override func viewDidLoad() {
super.viewDidLoad()
// Setup the pages
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let page1 = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "ThirdViewController")
pages.append(page1)
pages.append(page2)
pages.append(page3)
page1.variable = "This is strig..."
// Create the page container
pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageContainer.delegate = self
pageContainer.dataSource = self
pageContainer.setViewControllers([page1], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)
// Add it to the view
pageControllerView.addSubview(pageContainer.view)
// Configure our custom pageControl
view.bringSubviewToFront(pagerController)
pagerController.numberOfPages = pages.count
pagerController.currentPage = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - UIPageViewController delegates
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of:viewController)!
if currentIndex == 0 {
return nil
}
let previousIndex = abs((currentIndex - 1) % pages.count)
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of:viewController)!
if currentIndex == pages.count-1 {
return nil
}
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
pendingIndex = pages.firstIndex(of:pendingViewControllers.first!)
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed {
currentIndex = pendingIndex
if let index = currentIndex {
pagerController.currentPage = index
}
}
}
}
My `ViewConroller` code means which is loading in `PageViewController`
import UIKit
class ViewController: UIViewController {
var variable = String()
#IBOutlet weak var subView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Add gesture to incoming call view
let incomingCallsViewTap = UITapGestureRecognizer(target: self, action: #selector(incomingCallsViewTapFunction(_:)))
self.subView!.addGestureRecognizer(incomingCallsViewTap)
}
// Tap gestrure selector fuction
#objc func incomingCallsViewTapFunction(_ sender: UITapGestureRecognizer) {
let clvc = self.storyboard?.instantiateViewController(withIdentifier: "FVC") as! FinalViewController
self.navigationController?.pushViewController(clvc, animated: false)
}
#IBAction func btn(_ sender: Any) {
print("ViewController one")
print(variable)
}
}
navigationViewController is nil so I had to do the following:
(UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.pushViewController(clvc, animated: true)
// Tap gestrure selector fuction
#objc func incomingCallsViewTapFunction(_ sender: UITapGestureRecognizer) {
let clvc = self.storyboard?.instantiateViewController(withIdentifier: "FVC") as! FinalViewController
// self.navigationController?.pushViewController(clvc, animated: false)
(UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.pushViewController(clvc, animated: true)
}

How do I hide the status bar on my UIPageViewController? (All view controllers)

I've searched every question/answer I can find on here, but I can't figure out how to hide the status bar for all of the view controllers in my UIPageViewController. Here's the code from my UIPageViewController class:
class TipsVC: UIPageViewController, UIPageViewControllerDelegate {
lazy var VCArr: [UIViewController] = {
return [self.VCInstance(name: "T1"),
self.VCInstance(name: "T2"),
self.VCInstance(name: "T3")]
}()
private func VCInstance(name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
}
override public func viewDidLoad() {
super.viewDidLoad()
//self.dataSource = self
self.delegate = self
if let OB1 = VCArr.first {
setViewControllers([OB1], direction: .forward, animated: true, completion: nil)
let pageController = UIPageControl.appearance()
pageController.pageIndicatorTintColor = UIColor(red:1.00, green:0.88, blue:0.92, alpha:1.0)
pageController.currentPageIndicatorTintColor = UIColor(red:1.00, green:0.25, blue:0.51, alpha:1.0)
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
for view in self.view.subviews {
if view is UIScrollView {
view.frame = UIScreen.main.bounds
} else if view is UIPageControl {
view.backgroundColor = UIColor.clear
}
}
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = VCArr.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return VCArr.last
}
guard VCArr.count > previousIndex else {
return nil
}
return VCArr[previousIndex]
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = VCArr.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
guard nextIndex < VCArr.count else {
return VCArr.first
}
guard VCArr.count > nextIndex else {
return nil
}
return VCArr[nextIndex]
}
public func presentationCount(for pageViewController: UIPageViewController) -> Int{
return VCArr.count
}
public func presentationIndex(for pageViewController: UIPageViewController) -> Int{
guard let OB1 = viewControllers?.first,
let OB1Index = VCArr.index(of: OB1) else {
return 0
}
return OB1Index
}
public func nextPageWithIndex(index: Int)
{
let nextVC = VCArr[index]
setViewControllers([nextVC], direction: .forward, animated: true, completion: nil)
}
}
and here's a code sample from one of my viewControllers:
class T1: UIViewController {
#IBOutlet var nextBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
UIApplication.shared.isStatusBarHidden = true
}
#IBAction func nextBtnDidPress(_ sender: Any) {
let next = self.parent as! TipsVC
next.nextPageWithIndex(index: 1)
}
}
I'm well aware of how to hide the status bar on regular view controllers, but I'm unable to get the same results when I'm using a UIPageViewController. What's going on?
To hide statusbar from particluar viewController you need to hide status bar in viewWillAppear and hide status bar in viewWillDisAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.isStatusBarHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.isStatusBarHidden = false
}
viewWillAppear will call before opening particular viewController and viewWillDisAppear call after dismiss particular viewController
if you face problem with UIPageViewController use UIViewController and embed UIPageViewController inside that for reference check below code
var pageContainer: UIPageViewController!
pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageContainer.delegate = self
pageContainer.dataSource = self
pageContainer.setViewControllers([getViewControllerAtIndex(index: 0)] as [UIViewController], direction: .forward, animated: false, completion: nil)
pageContainer.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
self.view.addSubview(pageContainer.view)
Hope this will help you

How to properly advance a PageViewController in Swift

I am trying to develop a tutorial in Swift. I have a page view controller working with the indicators (http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/) but I want to add a button to go to the next page by clicking it instead of swiping the screen (TODO NextPage).
I've tried:
let startVC = self.viewControllerAtIndex(self.index + 1) as TutorialContentViewController
let viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as! [UIViewController],
direction: UIPageViewControllerNavigationDirection.Forward,
animated: true,
completion: nil)
The problem is that circle page indicator is not being updated and after clicking next, all the pages are the second page.
Source code
import UIKit
class TutorialViewController: UIViewController, UIPageViewControllerDataSource {
var pageViewController: UIPageViewController!
var pageTexts: NSArray!
var pageImages: NSArray!
var index: Int = 0
#IBOutlet var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.pageTexts = NSArray(objects: "Explore", "Today Widget")
self.pageImages = NSArray(objects: "background1.jpg", "background2.jpg")
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let startVC = self.viewControllerAtIndex(index) as TutorialContentViewController
let viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as! [UIViewController], direction: .Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.size.height - 60)
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 viewControllerAtIndex(index: Int) -> TutorialContentViewController {
if (index >= self.pageTexts.count) {
return TutorialContentViewController()
}
let vc: TutorialContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("TutorialContentViewController") as! TutorialContentViewController
vc.image = self.pageImages[index] as! String
vc.text = self.pageTexts[index] as! String
vc.pageIndex = index
self.index = index
self.button.setTitle(self.index == self.pageTexts.count - 1 ? "Finish" : "Next", forState: .Normal)
return vc
}
// MARK: - Page View Controller Data Source
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let vc = viewController as! TutorialContentViewController
let index = vc.pageIndex as Int
return self.viewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let vc = viewController as! TutorialContentViewController
var index = vc.pageIndex as Int
if (index == NSNotFound) {
return nil
}
index++
if (index == self.pageTexts.count) {
self.button.setTitle("Finish", forState: .Normal)
return nil
} else {
self.button.setTitle("Next", forState: .Normal)
}
return self.viewControllerAtIndex(index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return self.pageTexts.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
#IBAction func onButtonClick(sender: AnyObject) {
if (self.index == self.pageTexts.count - 1) {
let vc : UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("splashViewController")
vc.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.presentViewController(vc, animated: true, completion:nil)
} else {
//TODO NextPage
}
}
}
Here let viewControllers = NSArray(object: startVC) you make an array of UIViewController with only one UIViewController (your second UIViewController).
Instead use the viewControllerAtIndex function from your code.
So now your UIButton action should look like this:
func nextPage(sender: AnyObject) {
self.pageViewController.setViewControllers([self.viewControllerAtIndex(self.index + 1)!], direction: .Forward, animated: true, completion: nil)
}

How to call a controller from a modal popover in Swift?

I have three controllers, and i need this navigation:
| 1stVC | -> | popoverVC | -> | 2ndVC |
The first one show a modal view using a popover segue, and then from the modal view, using a protocol, it should show the second controller.
The protocol calls the method, and then, it should call the second controller, but it doesn't!. I have tried performing segue, and calling the controller but nothing happens, in fact, it reloads the first controller instead call the second. Please help. I think that i must have errors in delegation, but i can't figure what.(poor english, i know)
One important thing, the firstViewcontroller is called from another controller that is in a tabbarcontoller.
Thanks in advance.
This is the code:
PopoOverController:
import UIKit
protocol basketDelegate {
func didSelectValue()
}
class PopoverViewController: UITableViewController {
var delegate: basketDelegate!
let options = ["Filters", "Basket"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
let rdc = storyboard!.instantiateViewControllerWithIdentifier("FirstViewController") as! FirstViewController
self.delegate = rdc
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return options.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
cell.textLabel?.textAlignment = .Center
cell.textLabel?.textColor = colorWithHexString("#1C7393")
cell.textLabel?.text = options[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
delegate?.didSelectValue()
}
}
FirstController
import UIKit
class FirstViewController: AEAccordionTableViewController,
UIPopoverPresentationControllerDelegate, UISearchBarDelegate,basketDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var Filters = PopoverViewController()
Filters.delegate = self
}
// MARK: - Popup filter call
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "popoverSegue" {
let popoverViewController = segue.destinationViewController as UIViewController
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverViewController.popoverPresentationController!.delegate = self
}
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
func didSelectValue() {
//Option 1--Failed!
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil);
let SecondViewControllerObject : UIViewController = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController;
self.presentViewController(SecondViewControllerObject, animated: true, completion: nil);
//Option 2--Failed
self.performSegueWithIdentifier("secondSegue", sender: self)
//Option 3--Failed
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let SecondViewControllerObject : UIViewController = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController;
let navigationController = UINavigationController(rootViewController: SecondViewControllerObject)
self.presentViewController(navigationController, animated: true, completion: nil)
//Option 4--Failed
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
FirstViewController()!.presentViewController(vc, animated: true, completion: nil)
//Option 5--Failed
self.navigationController?.presentViewController(vc, animated: false, completion: nil)
}
}
SecondController
import UIKit
class SecondViewController: AEAccordionTableViewController {
#IBOutlet weak var dismissButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func dismissTap(sender: UIButton) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
Your first view controller should be a PopoverViewControllerDelegate, not a basketDelegate.
This behavior can be reached with an Action Sheet. Just in the First Controller add a new method to create a Contextual Menu with the same option to call the second controller. This method is called from a button.
Is a simple solution, without protocols or delegations.
In the first controller:
#IBAction func showSheetASction(sender: UIBarButtonItem) {
print("click")
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
print(action)
}
alertController.addAction(cancelAction)
let CallSVCAction = UIAlertAction(title: "Filters", style: .Default) { (action) in
// ...
print(action)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
self.presentViewController(vc, animated: true, completion: nil)
}
alertController.addAction(CallSVCAction)
alertController.view.tintColor = colorWithHexString("#1C7393")
self.presentViewController(alertController, animated: true) {
}
}

Swift - UITapGestureRecognizer UIPageViewController

Okay, I've created a pageview slider app. Everything works great, but now I'd like to be able to hide/unhide my navbar with a single tap using UITapGestureRecognizer.
I have been able to hide the navbar on viewDidLoad, but I am not sure where to call my singleTapped function, or how to implement UITapGestureRecognizer.
How can I hide/unhide the UIPageViewController navbar with UITapGestureRecognizer?
New to swift/ios
my code
class ViewController: UIViewController, UIPageViewControllerDataSource {
var pageViewController: UIPageViewController!
private var allPages = [Page]()
var pages = NSMutableOrderedSet()
override func viewDidLoad() {
super.viewDidLoad()
###Hide NavBar
self.navigationController?.navigationBarHidden = true
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
var startVC = self.viewControllerAtIndex(0) as ContentViewController
var viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
###Function to Hide/Unhide Navbar
func singleTapped(recognizer: UITapGestureRecognizer) {
if (self.navigationController?.navigationBarHidden == false) {
###hide the Navigation Bar
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
###if Navigation Bar is already hidden
else if (self.navigationController?.navigationBarHidden == true)
{
###Show the Navigation Bar
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
var vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if (index == 0 || index == NSNotFound) {
return nil
}
index--
return self.viewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
var vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if (index == NSNotFound) {
return nil
}
index++
if (index == self.pages.count) {
return nil
}
return self.viewControllerAtIndex(index)
}
func viewControllerAtIndex(index: Int) -> ContentViewController
{
if ((self.pages.count == 0) || (index >= self.pages.count)) {
return ContentViewController()
}
var vc: ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController
###Store image in imageFile
var image = (pages.objectAtIndex(index) as! Page).image
vc.imageFile = image
vc.pageIndex = index
return vc
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return self.pages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 0
}
}
Use UITapGestureRecognizer:
In viewDidLoad function add
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action:"singleTapped:"))

Resources