子视图滚动不能正常工作SWIFT

时间:2016-08-08 17:54:49

标签: ios swift2 uipageviewcontroller addsubview

目标:使viewcontroller具有多个页面,并且可以通过分段控制器进行交换,页面内容可以垂直滚动

细节:

  • 我制作了一个pagviewcontroller并将其作为子视图嵌入到主视图控制器中

    //add pageviewcontroller as subview to viewcontroller
    if let vc = storyboard?.instantiateViewControllerWithIdentifier("ProfileEditController"){
    
        self.addChildViewController(vc)
        self.view.addSubview(vc.view)
    
        EditTabs = vc as! UIPageViewController
        EditTabs.dataSource = self
        EditTabs.delegate = self
    
        //define First page
        EditTabs.setViewControllers([pagesAtIndexPath(0)!], direction:.Forward, animated: true, completion: nil)
        EditTabs.didMoveToParentViewController(self)
    
        //bring segmented view buttons to front of pageViews
        self.view.bringSubviewToFront(self.topTabs)
    }
    
  • 我调用了pageViewController函数,我通过恢复标识符添加页面

  • 我通过获取pageindex并设置viewcontroller来管理分段视图控制器:

    EditTabs.setViewControllers([pagesAtIndexPath(0)!], direction:.Reverse, animated: true, completion: nil)
    
  • 故事板中的
  • 子页面内部有滚动视图以保存内容

  • 我通过segue调用子页滚动视图并且工作正常

案例:

  • 一切正常只有子页面的滚动视图根本不起作用

如何解决这个问题? 非常感谢您的指导

谢谢,

2 个答案:

答案 0 :(得分:5)

  1. 我创建了一个带有PageViewController的视图控制器,其中有三个视图控制器,其中包含滚动视图。它工作正常。

    导入UIKit class ViewController:UIViewController,UIPageViewControllerDataSource {

    var viewControllers : [UIViewController]?
    override func viewDidLoad() {
        super.viewDidLoad()
        CreatePageView()
    }
    
    func CreatePageView() {
    
        SetupViewControllers()
    
        let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        pageViewController.dataSource = self
        pageViewController.setViewControllers([(viewControllers?[0])!] , direction: .forward, animated: false, completion: nil)
        pageViewController.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height);
        pageViewController.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height);
        addChildViewController(pageViewController)
        view.addSubview(pageViewController.view)
        pageViewController.didMove(toParentViewController: self)
        pageViewController.view.backgroundColor = UIColor.blue
    }
    
    func SetupViewControllers() {
        let firstVC = UIViewController()
        firstVC.view.tag = 100
        firstVC.view.backgroundColor = UIColor.red
        AddScrollView(bgView: firstVC.view)
        let secondVC = UIViewController()
        secondVC.view.tag = 101
        secondVC.view.backgroundColor = UIColor.brown
        AddScrollView(bgView: secondVC.view)
        let thirdVC = UIViewController()
        thirdVC.view.tag = 102
        thirdVC.view.backgroundColor = UIColor.purple
        AddScrollView(bgView: thirdVC.view)
    
        viewControllers = [firstVC,secondVC,thirdVC]
    
    }
    
    func AddScrollView(bgView: UIView) {
        let scrollView = UIScrollView()
        scrollView.frame = CGRect.init(x: 10, y: 10, width: bgView.frame.width-20, height: bgView.frame.height-20)
        scrollView.backgroundColor = UIColor.init(red: 0.34, green: 0.45, blue: 0.35, alpha: 0.9)
        bgView.addSubview(scrollView)
        scrollView.contentSize = CGSize.init(width: scrollView.frame.size.width, height: scrollView.frame.size.height+200)
    }
    
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        if viewController.view.tag == 101 {
            return viewControllers?[0]
        }
        else if viewController.view.tag == 102{
            return viewControllers?[1]
        }
        else{
            return viewControllers?[2]
        }
    }
    
    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        if viewController.view.tag == 101 {
            return viewControllers?[0]
        }
        else if viewController.view.tag == 102{
            return viewControllers?[1]
        }
        else{
            return viewControllers?[2]
        }
    }
    
    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return (viewControllers?.count)!
    }
    
    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return 0
    }
    

    }

答案 1 :(得分:1)

在Stack上经常会出现类似的问题。对于我自己在非常相似的情况下的问题,我也能够在其自己的视图中使用scrollview而没有问题,但是当在另一个子视图中用作子视图时,我需要以编程方式设置scrollview维度。

Swift 2

scrollView.setFrame(CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT))

Swift 3

scrollView.setFrame(CGRect(x: 0, y: 0, width: DEVICE_WIDTH, height: DEVICE_HEIGHT))

进一步(通常被视为更好)的建议,根据内容自动设置大小。

var contentRect = CGRectZero
for view in self.scrollView.subviews {
    contentRect = CGRectUnion(contentRect, view.frame)
}
self.scrollView.contentSize = contentRect.size 

希望这会有所帮助。