PageViewController in NavigationController child ViewController scrolling issues in ios

  • Navigation Controller - Parent
  • Page ViewController - Child navigation controller
  • DetailsViewController - The ViewController will be displayed in the PageViewController.

My DetailsViewController contains scrollview, and the detailsviewController array with different data is passed to the viewcontroller page.

The problem is only in the first view. The pageview controller is displayed under the navigation bar and its scrollview is working.

But the rest will move to the navigation controller, and the scroll function does not work.

The controller for viewing the first information correctly

Next DetailsViewController from the dataset when I scroll.  Here the content is placed in the navigation bar and scrollview is working

1st image: the first data view controller works correctly

2nd Image: Next DetailsViewController , . ,

, navigationcontroller viewviewcontroller, , nil.

 //
//  PagerViewController.swift
//  
//
//  Created by Admin on 13/07/16.
// 
//

import UIKit

class PagerViewController: UIPageViewController {

    var result =  Array<SearchResult>();
    var mainStoryBoard  = UIStoryboard(name: "Main", bundle: nil);
    var resultSet = [UIViewController]();
    var currentIndex = 0;
    // MARK: - Life cycle Methods
    override func viewDidLoad() {
        super.viewDidLoad();
        dataSource = self;
        delegate = self;
        generateDatset();
        let initialViewController  = resultSet[currentIndex] ;
        let viewControllers = NSArray(objects : initialViewController);
        setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil);
        self.navigationController!.edgesForExtendedLayout = .None;
    }

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

    // MARK: - Dataset Initialisation

    func generateDatset()  {

        for searchResult in result {
            let viewController  = mainStoryBoard.instantiateViewControllerWithIdentifier(Constants.STORYBOARDUIFIELDS.DETAILSVIEWCONTROLLER) as? DetailsViewController;

            viewController?.searchResult =  searchResult;
            resultSet.append(viewController!)
        }

    }






}

// MARK: - UIPageViewControllerDataSource

extension PagerViewController : UIPageViewControllerDataSource {

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


        guard let viewControllerIndex  = resultSet.indexOf(viewController)  else {
            return nil;
        }

        let nextIndex  = viewControllerIndex + 1;

        guard  resultSet.count > nextIndex else {
                       return nil;
        }

        currentIndex = nextIndex;
        return resultSet[nextIndex];


    }

    func scrollToNext(viewControllers : UIViewController) {
        setViewControllers([viewControllers], direction: .Forward, animated: true, completion: nil)
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
       guard let viewControllerIndex = resultSet.indexOf(viewController) else {
            return nil;
        }
        let previousIndex =  viewControllerIndex - 1;

        guard previousIndex >= 0  else {
            return nil;
        }
        currentIndex  = previousIndex;
        return resultSet[previousIndex];

    }

}


 //
//  DetailsViewController.swift
//
//
//  Created by Admin on 13/07/16.
//  
//

import UIKit

class DetailsViewController: UIViewController {


    var searchResult  : SearchResult? = nil;

    @IBOutlet weak var ShoeImageView: UIImageView!

    @IBOutlet weak var DnsItemCodeLabel: UILabel!

    @IBOutlet weak var GeneralDetailsCollView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        //print(navigationController)

        DnsItemCodeLabel.text = "\(searchResult!.DNS!) - \(searchResult!.ItemCode!)";

        // converting base64encoded image into nsdata and then to ui image

        let image = UIImage(data: NSData(base64EncodedString: searchResult!.Image!, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!)

        ShoeImageView.image = image;

        // Assigning source to CollectionView

        GeneralDetailsCollView.dataSource = self;
        print(navigationController)

    }

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





}

// MARK: - UICollectionViewDataSource

extension DetailsViewController : UICollectionViewDataSource  {


    // returns the number of items

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (searchResult!.summaryItems?.count)!;
    }

    //return the cell for given position

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
          let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Constants.STORYBOARDUIFIELDS.GENRAL_DETAILS_CELL, forIndexPath: indexPath) as! GeneralDetailsCell
        if let data = searchResult?.summaryItems![indexPath.row] {
            cell.TitleLable.text =  data.Title!;
            cell.ValueLabel.text  = data.Value!;
        }

        return cell

    }


}
+4
4

. , , ViewController NavigationViewController. PageViewController ViewController. . , , NavigationController ViewController PageViewController.

pageViewController viewController.swift(root View Controller ), ViewController.swift :

import UIKit

class ViewController: UIViewController,UIPageViewControllerDataSource,UIPageViewControllerDelegate {
    var pageViewController:UIPageViewController?
    var presentIndex = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        self.pageViewController = UIPageViewController.init(transitionStyle: UIPageViewControllerTransitionStyle.Scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal, options: nil)
        self.pageViewController!.dataSource=self
        let initialViewController:DetailViewController = self.giveRequiredViewController(1)
        let viewControllerArray:NSArray = [initialViewController]
        self.pageViewController?.setViewControllers(viewControllerArray as! [DetailViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
        self.addChildViewController(self.pageViewController!)
        self.view.addSubview((self.pageViewController?.view)!)
        self.pageViewController?.didMoveToParentViewController(self)
        self.view.backgroundColor=UIColor.blackColor()
        self.navigationController?.navigationBar.barTintColor=UIColor.redColor()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillLayoutSubviews() {
        let frame = CGRectMake(0, 64, self.view.frame.width, self.view.frame.height-40)
        self.pageViewController?.view.frame = frame
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        presentIndex = (viewController as! DetailViewController).index!
        if presentIndex==0{
            return nil
        }
       return self.giveRequiredViewController(presentIndex-1)
    }

    func giveRequiredViewController(index:NSInteger) -> DetailViewController {
        let detailViewController = DetailViewController(nibName: "DetailViewController", bundle: nil)
        detailViewController.index = index
        return detailViewController
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        presentIndex = (viewController as! DetailViewController).index!
        if presentIndex==1{
            return nil
        }
        return self.giveRequiredViewController(presentIndex+1)
    }

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


}

DetailViewController, , , . . , , .

DetailViewController.swift :

import UIKit

class DetailViewController: UIViewController {
    var index:NSInteger?
    override func viewDidLoad() {
        super.viewDidLoad()
        if index==0{
            self.view.backgroundColor=UIColor.blueColor()
        }else{
            self.view.backgroundColor = UIColor.grayColor()
        }
        // Do any additional setup after loading the view.
    }

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

}

+3

self.edgesForExtendedLayout =.None .

0
 self.edgesForExtendedLayout = .None
self.automaticallyAdjustsScrollViewInsets = false;
0

PageViewController

self.automaticallyAdjustsScrollViewInsets = false;

DetailsViewController AdjustsScrollViewInsets.

Make sure that both the PageViewController and DetailViewController parameters are set to "Extend edges": "Below the upper bars (checked)", "Below the lower bars (checked)" and "Under the opaque bars" ( Not checked ) "(from the Builder interface or the source code).

0
source

Source: https://habr.com/ru/post/1648515/


All Articles