PageViewController transparent or no background - swift

I have problem with PageViewController dots indicators. Actually with background. I tried UIColor.clear but this didn't help. Background still white. I can change the color but I don't know how to remove it or make it transparent.
Can't find a way how to make it transparent.
Screenshot attached.
How to make background color transparent or remove it?
ViewController.swift
import Foundation
import UIKit
class ViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
// Hide status bar.
override var prefersStatusBarHidden : Bool {
return true
}
var pageViewController: UIPageViewController!
let pages = ["DomesticPageViewController", "SafariPageViewController", "ForestPageViewController"]
// Page View Controller Datasource
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let index = pages.index(of: viewController.restorationIdentifier!) {
if index > 0 {
return viewControllerAtIndex(index - 1)
}
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let index = pages.index(of: viewController.restorationIdentifier!) {
if index < pages.count - 1 {
return viewControllerAtIndex(index + 1)
}
}
return nil
}
func viewControllerAtIndex(_ index: Int) -> UIViewController? {
let vc = storyboard?.instantiateViewController(withIdentifier: pages[index])
return vc
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let vc = storyboard?.instantiateViewController(withIdentifier: "MyPageViewController") {
self.addChildViewController(vc)
self.view.addSubview(vc.view)
pageViewController = vc as! UIPageViewController
pageViewController.dataSource = self
pageViewController.delegate = self
pageViewController.setViewControllers([viewControllerAtIndex(0)!], direction: .forward, animated: true, completion: nil)
pageViewController.didMove(toParentViewController: self)
}
}
private func setupPageControl() {
let appearance = UIPageControl.appearance()
appearance.pageIndicatorTintColor = UIColor.lightGray
appearance.currentPageIndicatorTintColor = UIColor.black
appearance.backgroundColor = UIColor.clear
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
setupPageControl()
return self.pages.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
DomesticViewController.swift
import Foundation
import UIKit
class DomesticPageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "domestic_bg1")
backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

Related

How to stop after view scroll in page controller at particular view

I integrated pageviewcontroller using storyboard. i have 4 view. how to stop after scroll at third view. how to disable after scoll?
This my code. how to stop after scroll in ThirdViewController.
import UIKit
class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
var pageControl = UIPageControl()
// MARK: UIPageViewControllerDataSource
lazy var orderedViewControllers: [UIViewController] = {
return [self.newVc(viewController: "sbRed"),self.newVc(viewController: "sbBlue"),self.newVc(viewController: "sbOrange"),self.newVc(viewController: "sbGray")]
}()
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
// This sets up the first view that will show up on our page control
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
configurePageControl()
// Do any additional setup after loading the view.
}
func configurePageControl() {
// The total number of pages that are available is based on how many available colors we have.
pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
self.pageControl.numberOfPages = orderedViewControllers.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.black
self.pageControl.pageIndicatorTintColor = UIColor.white
self.pageControl.currentPageIndicatorTintColor = UIColor.black
self.view.addSubview(pageControl)
}
func newVc(viewController: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
}
// MARK: Delegate methords
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
let pageContentViewController = pageViewController.viewControllers![0]
self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!
}
// MARK: Data source functions.
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
// User is on the first view controller and swiped left to loop to
// the last view controller.
guard previousIndex >= 0 else {
return orderedViewControllers.last
// Uncommment the line below, remove the line above if you don't want the page control to loop.
// return nil
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
// User is on the last view controller and swiped right to loop to
// the first view controller.
guard orderedViewControllersCount != nextIndex else {
return orderedViewControllers.first
// Uncommment the line below, remove the line above if you don't want the page control to loop.
// return nil
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
}
import UIKit
class ThirdViewController: UIViewController {
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.
}
}
You need to use UiPagerViewDatasource methods :
a) func pageViewController(UIPageViewController, viewControllerBefore: UIViewController)
b) func pageViewController(UIPageViewController, viewControllerAfter: UIViewController)
Example:
func pageViewController(UIPageViewController, viewControllerBefore: UIViewController){
if viewController == controllerA {
return controllerB;
}
else if viewController == controllerB {
return controllerC;
}
else ifviewController == controllerC {
return nil;
}}
and
func pageViewController(UIPageViewController, viewControllerAfter: UIViewController){
if (viewController == controllerC) {
return controllerB;
}
else if(viewController == controllerB) {
return controllerA;
}
else {
return nil;
}}
Sets the view controllers to be displayed you need to call :
pager.setViewControllers([selectingViewController]?, direction: UIPageViewControllerNavigationDirectionForward, animated: false, completion:nil)

How to embed multiple videos in swift?

How to create array of multiple mp4 or mov files in swift. I was able to display the single video in uiwebview. I have pagecontrol to display some text but I need to display different videos same like texts. When the page control starts it should display next video. Here is the code for first view controller and page view controller.
import UIKit
class ViewController: UIViewController,UIPageViewControllerDataSource {
//var pageImages:NSArray!
var ouotes: NSArray!
var video: NSArray!
var pageViewController:UIPageViewController!
#IBOutlet weak var GenerateNumbers: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
video = ["2.mov", "1.mov", "4.mov"]
ouotes = ["sometext" ]
// NSArray(objects:"ap", "bg", "gfb")
/* UIGraphicsBeginImageContext(self.view.frame.size)
UIImage(named: "money")?.draw(in: self.view.bounds)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
self.view.backgroundColor = UIColor(patternImage: image)*/
self.pageViewController = self.storyboard?.instantiateViewController(withIdentifier: "MyPageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let initialContenViewController = self.pageTutorialAtIndex(0) as ContentHolder
self.pageViewController.setViewControllers([initialContenViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: self.view.frame.size.height-100)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMove(toParentViewController: self)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pageTutorialAtIndex(_ index: Int) -> ContentHolder {
let pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "ContentHolder") as! ContentHolder
pageContentViewController.imageFileName = ouotes[index] as! String
pageContentViewController.videoFileName = video [index] as! String
pageContentViewController.pageIndex = index
return pageContentViewController
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
{
let viewController = viewController as! ContentHolder
var index = viewController.pageIndex as Int
if (index == 0 || index == NSNotFound) {
return nil
}
index -= 1
return self.pageTutorialAtIndex(index)
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
let viewController = viewController as! ContentHolder
var index = viewController.pageIndex as Int
if ((index == NSNotFound)) {
return nil
}
index += 1
if (index == ouotes.count) {
return nil
}
if (index == video.count) {
return nil
}
return self.pageTutorialAtIndex(index)
}
public func presentationCount(for pageViewController: UIPageViewController) -> Int {
return ouotes.count
}
public func presentationIndex(for pageViewController: UIPageViewController) -> Int{
return 0
}
}
And for the pagecontrol view where texts and videos should be displayed
import Foundation
import UIKit
import AVKit
import AVFoundation
class ContentHolder: UIViewController {
var imageFileName: String!
var videoFileName: String!
var pageIndex:Int!
#IBOutlet weak var EuroScrollView: UIScrollView!
#IBOutlet weak var VideoView: UIWebView!
#IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// let fileURL = NSURL (fileURLWithPath: "/Users/nafu/Desktop/2.mov")
VideoView.loadHTMLString("<iframe width = \"\(self.VideoView.frame.width) \" height =\"\(self.VideoView.frame.height)\" src = \"\(videoFileName)\"> </iframe>", baseURL: nil)
myLabel.text = imageFileName
myLabel.numberOfLines = 0
myLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
myLabel.font = UIFont(name:"HelveticaNeue-Bold", size: 15.0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Thanks in advance.

Transition between View Controllers with button (UIPageViewController Items)

I have 2 UIViewControllers as items of UIPageViewController. It works well for slide, but i want to move from pageOne to pageTwo with button's action. How i can do that?
Here is my code and storyboard preview:
Contents of Pager.swift :
import UIKit
class Pager: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource, UIScrollViewDelegate
{
var viewsArray: [UIViewController] = []
var pageOne: UIViewController = UIViewController()
var pageTwo: UIViewController = UIViewController()
var currentIndex = 0
var previousIndex = 0
var nextIndex = 0
override func viewDidLoad()
{
super.viewDidLoad()
self.delegate = self
self.dataSource = self
pageOne = (storyboard?.instantiateViewController(withIdentifier: "pageOne"))!
pageTwo = (storyboard?.instantiateViewController(withIdentifier: "pageTwo"))!
viewsArray.append(pageOne)
viewsArray.append(pageTwo)
setViewControllers([pageOne], direction: .forward, animated: true, completion: nil)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
currentIndex = viewsArray.index(of: viewController)!
if currentIndex == 1
{
return nil
}
else
{
nextIndex = abs((currentIndex + 1) % viewsArray.count)
return viewsArray[nextIndex]
}
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
{
currentIndex = viewsArray.index(of: viewController)!
if currentIndex == 0
{
return nil
}
else
{
previousIndex = abs((currentIndex - 1) % viewsArray.count)
return viewsArray[previousIndex]
}
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return viewsArray.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 0
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
Contents of PageOne.swift :
import UIKit
class PageOne: UIViewController
{
var pager = Pager()
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBAction func forwardButton(_ sender: Any)
{
pager.setViewControllers([pager.pageOne], direction: .forward, animated: true, completion: nil)
}
}
You need to write below line
pageController?.setViewControllers([YOUR_VIEW_CONTROLLER], direction: direction, animated: true, completion: nil)
give proper direction either forward or reverse based on which controller you are at
hope this will help you
You need to pass pager to your both viewcontrollers
pageOne.pager = self
pageTwo.pager = self

How to retrieve value from extension in view controller swift

I want to retrieve the value from the scrollViewDidScroll function at my view controller. It gives the right value back in the console so that's nice.
My view controller:
import UIKit
extension UIPageViewController: UIScrollViewDelegate {
public override func viewDidLoad() {
super.viewDidLoad()
for subView in view.subviews {
if subView is UIScrollView {
(subView as! UIScrollView).delegate = self
}
}
}
public func scrollViewDidScroll(scrollView: UIScrollView) {
let point = scrollView.contentOffset
var percentComplete: CGFloat
percentComplete = fabs(point.x - view.frame.size.width)/view.frame.size.width
print("percentComplete: ", percentComplete)
}
}
class StepsDetailViewController: UIViewController {
var pageIndex: Int!
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Where the view controller came from:
import UIKit
class PageControl: UIPageViewController, UIPageViewControllerDataSource {
var pageViewController: UIPageViewController!
var pageTitles: [[String]]!
override func viewDidLoad(){
super.viewDidLoad()
testC().getRecent() { result, error in
self.pageTitles = result;
self.pageViewController = self
self.pageViewController.dataSource = self
let startVC = self.viewControllerAtIndex(0) as StepsDetailViewController
dispatch_async(dispatch_get_main_queue(), {
let viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)
})
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewControllerAtIndex(index: Int) -> StepsDetailViewController
{
if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
return StepsDetailViewController()
}
let vc: StepsDetailViewController = self.storyboard?.instantiateViewControllerWithIdentifier("StepsDetailViewController") as! StepsDetailViewController
vc.pageIndex = index
return vc
}
// MARK: - Page View Controller Data Source
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
let vc = viewController as! StepsDetailViewController
var index = vc.pageIndex as Int
if (index == 0 || index == NSNotFound)
{
return nil
}
index -= 1
return self.viewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let vc = viewController as! StepsDetailViewController
var index = vc.pageIndex as Int
if (index == NSNotFound)
{
return nil
}
index += 1
if (index == self.pageTitles.count)
{
return nil
}
return self.viewControllerAtIndex(index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return self.pageTitles.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 0
}
}
Is this possible?
You could do like this:
1. Replace your extension with a custom UIPageViewController
class BasePageViewController: UIPageViewController, UIScrollViewDelegate {
var percentComplete: CGFloat = 0.0 {
didSet { self.percentCompleteDidChange() }
}
func percentCompleteDidChange() {
print("percentCompleteDidChange")
}
public override func viewDidLoad() {
super.viewDidLoad()
for subView in view.subviews {
if subView is UIScrollView {
(subView as! UIScrollView).delegate = self
}
}
}
public func scrollViewDidScroll(scrollView: UIScrollView) {
let point = scrollView.contentOffset
self.percentComplete = fabs(point.x - view.frame.size.width)/view.frame.size.width
print("percentComplete: ", percentComplete)
}
}
2. PageControl is a BasePageViewController subclass
class PageControl: BasePageViewController, UIPageViewControllerDataSource {
// here you have the access to percentComplete of superclass
func printPercentComplete() {
if let percentComplete = self.percentComplete {
print(percentComplete)
}
}
override func percentCompleteDidChange() {
print("percentCompleteDidChange from child")
}
......
}
Define the percentComplete as a property of your custom class that inherits from UIPageViewController.
Then you can access it everywhere.

How to Enable scroll in PageViewController after disable delay in button highlighted in SWIFT 2.0

I just have trouble with button highlighted delay in UIPageViewController.
I can solve now by setting property "delaysContentTouches" = false to subview.
But now i can't scroll page anymore. i search forum and found "gestureRecognizers" which might help. but i don't know how to set.
could you help suggest me?
import UIKit
class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pages = [UIViewController]()
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
//declare all pages
let page1: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("c01")
let page2: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("c02")
let page3: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("c03")
pages.append(page1)
pages.append(page2)
pages.append(page3)
setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}
override func viewDidAppear(animated: Bool) {
for view in self.view.subviews {
if view is UIScrollView {
(view as? UIScrollView)!.delaysContentTouches = false
}
}
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = pages.indexOf(viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return pages.last
}
guard pages.count > previousIndex else {
return nil
}
return pages[previousIndex]
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = pages.indexOf(viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = pages.count
guard orderedViewControllersCount != nextIndex else {
return pages.first
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return pages[nextIndex]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I can do it now.
It is not possible for uipageviewcontroller.
i have to move to uiscrollviewcontroller
then subclass it and override function touchesShouldCancelInContentView for all my UIButton to be true.
now i can tap button without delay and can still hold my finger to scroll.