NavigationController 子项 ViewController 中的页面ViewController ios 中的滚动问题

PageViewController in NavigationController child ViewController of scrolling issue in ios

  1. 导航控制器 - 父级
  2. 页面 ViewController - 导航控制器的子项
  3. 详细信息ViewController - ViewController 显示在页面ViewController。

我的详细信息ViewController 包含滚动视图和详细信息数组viewController 将不同的数据传递给 pageviewcontroller。

问题只是第一个 viewController 的 pageviewcontroller 显示在 navigartionbar 下,并且它的 scrollview 正在工作。

但其余部分被推送到导航控制器下,滚动视图不工作。

第一张图片:第一个细节视图控制器正常工作

第二张图片:我滑动时来自数据集的下一个详细信息ViewController。这里的内容被推送到导航栏和滚动视图下不工作

经过进一步研究后,我发现导航控制器 属性 除第一个之外的其余 detailsviewcontroller 为零。

 //
//  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

    }


}

在父视图控制器中使用self.edgesForExtendedLayout = .None。工作正常

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

我遇到过一次,所以我当时就是加了一个普通的ViewController,然后让它成为导航的根控制器ViewController。现在,我在这个 ViewController 中添加了 PageViewController。然后一切正常。简而言之,尽管将 NavigationController 作为父控制器,但将 Navigation Controller 的根 ViewController 作为 PageViewController.

的父级

我在viewController.swift(导航控制器的根视图控制器)中制作了页面ViewController实例,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.
    }


}

我为详细信息提供了一个索引ViewController,这个索引基本上会告诉我正在显示哪个页面,我会相应地调整我的视图。现在我已经更改了视图更改的背景颜色。 目前只有两页,有多少就多多少。

详情ViewController.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.
    }

}

完整的代码可以在这个link

中找到

尝试配置您的 PageViewController 实例:

self.automaticallyAdjustsScrollViewInsets = false;

对于 DetailsViewController,automaticallyAdjustsScrollViewInsets 应该为真。

确保 PageViewController 和 DetailViewController 都具有 "Extend Edges" 设置,如下所示:"Under Top Bars (Checked)"、"Under Bottom Bars (Checked)" 和 "Under Opaque Bars (Un-checked)"(来自 Interface Builder 或您的源代码)。