swift2 导航控制器子视图控制器中的页面视图控制器在ios中存在滚动问题

dba5bblo  于 2022-11-06  发布在  Swift
关注(0)|答案(4)|浏览(313)

1.导航控制器-父级
1.页面视图控制器-导航控制器的子级

  1. DetailsViewController -要在页面视图控制器中显示视图控制器
    我的DetailsViewController包含scrollview和detailsViewController的数组,其中不同的数据被传递给pageviewcontroller。
    问题是只有pageviewcontroller的第一个viewController显示在navigationbar下,并且其scrollview工作正常。
    但其余部分被推到导航控制器下,滚动视图不工作。

第1张图:第一个详细信息视图控制器工作正常
第二张图片:当我滑动时,数据集中的下一个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

    }

}
xyhw6mcr

xyhw6mcr1#

我曾经遇到过这样的情况,所以我当时做的是,我添加了一个普通的ViewController,并使其成为NavigationViewController的根控制器。现在,我在这个ViewController中添加了PageViewController。然后一切都运行正常。简而言之,尽管将NavigationController作为父控制器,但仍将Navigation Controller的根ViewController作为PageViewController的父控制器。
我已经在viewController.swift(导航控制器的根视图控制器)中创建了pageViewController示例,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提供了一个索引,这个索引基本上会告诉我显示的是哪个页面,我会相应地调整视图。现在我已经更改了视图更改的背景颜色。目前只有两个页面,你可以有尽可能多的页面。
详细视图控制器。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中找到

nuypyhwy

nuypyhwy2#

请尝试使用以下内容配置PageViewController示例:

self.automaticallyAdjustsScrollViewInsets = false;

对于DetailsViewController,自动调整滚动视图插入值应为true。
请确保PageViewController和DetailViewController都具有“扩展边缘”设置,如下所示:“顶部条下(选中)"、“底部条下(选中)”和“不透明条下(未选中)”(来自Interface Builder或源代码)。

1tuwyuhd

1tuwyuhd3#

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

fnvucqvd4#

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

相关问题