我的DetailsViewController包含scrollview和detailsviewController数组,其中不同的数据被传递给pageviewcontroller。
问题是第一个viewview控制器的viewController显示在navigartionbar下,其scrollview正在工作。
但是在导航控制器下按下了,滚动视图无效。
第一张图片:First Details视图控制器正常工作
第二张图片:当我滑动时,数据集中的下一个DetailsViewController。这里的内容在导航栏下滚动,滚动视图无法正常工作
在做了进一步的研究后,我发现除了first之外,其余的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
}
}
答案 0 :(得分:3)
这曾经发生在我身上,所以当时我做的是,我添加了一个普通的 ViewController并使其成为NavigationViewController的根控制器。现在,我在这个ViewController中添加了PageViewController。然后一切都很好。简而言之,尽管将NavigationController作为父控制器使导航控制器的根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提供了一个索引,这个索引基本上会告诉我正在呈现哪个页面,我将相应地调整我的视图。现在我已经更改了视图更改的背景颜色。 目前只有两页,你可以拥有尽可能多的页面。
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
中找到完整的代码答案 1 :(得分:0)
在父视图控制器中使用 self.edgesForExtendedLayout = .None 。它工作正常
答案 2 :(得分:0)
self.edgesForExtendedLayout = .None
self.automaticallyAdjustsScrollViewInsets = false;
答案 3 :(得分:0)
尝试使用以下命令配置PageViewController实例:
self.automaticallyAdjustsScrollViewInsets = false;
对于DetailsViewController,automaticAdjustsScrollViewInsets应为true。
确保PageViewController和DetailViewController都具有&#34; Extend Edges&#34;设置如下:&#34;在顶栏下(已选中)&#34;,&#34;在底栏下(已选中)&#34;和&#34;在不透明的酒吧(未经检查)&#34; (来自Interface Builder或您的源代码)。