gpt4 book ai didi

ios - ios中滚动问题的NavigationController子ViewController中的PageViewController

转载 作者:可可西里 更新时间:2023-11-01 05:43:13 25 4
gpt4 key购买 nike

  1. 导航 Controller - 父级
  2. Page ViewController - 导航 Controller 的子级
  3. DetailsViewController - 要在 PageViewController 中显示的 ViewController。

我的 DetailsViewController 包含 ScrollView 和具有不同数据的 detailsviewController 数组传递给 pageviewcontroller。

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

但其余部分被推送到导航 Controller 下, ScrollView 无法正常工作。

First Details view controller working properly

Next DetailsViewController from dataset when i swipe. Here the content is pushed under navigation bar and scrollview not working

第一张图片:第一个细节 View Controller 正常工作

第二张图片:当我滑动时数据集中的下一个 DetailsViewController。这里的内容被推送到导航栏下, ScrollView 不起作用

经过进一步研究,我发现除第一个之外的其余 detailsviewcontroller 的 navigationcontroller 属性为 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

}


}

最佳答案

这在我身上发生过一次,所以当时我做了什么,我添加了一个普通的 ViewController 并使它成为 NavigationViewController 的根 Controller 。现在,我在这个 ViewController 中添加了 PageViewController。然后一切正常。简而言之,尽管将 NavigationController 作为父 Controller ,但还是将 Navigation Controller 的根 ViewController 作为 PageViewController 的父 Controller 。

我在 viewController.swift(导航 Controller 的 Root View Controller )中制作了 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 提供了一个索引,这个索引基本上会告诉我正在显示哪个页面,我会相应地调整我的 View 。现在我已经更改了 View 更改的背景颜色。目前只有两页,你可以拥有多少。

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中找到

关于ios - ios中滚动问题的NavigationController子ViewController中的PageViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38482816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com