Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subview scroll is not working SWIFT

Goal: to make a viewcontroller have multiple pages and can be swapped through a segmented controller, pages content are scrollable vertically

details:

  • I made a pagviewcontroller and embedded it as a subview to main viewcontroller

    //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)
    }
    
  • I called pageViewController functions, and I am adding pages through restoration Identifiers

  • I managed segmented view controller by getting pageindex and setting viewcontroller like this:

    EditTabs.setViewControllers([pagesAtIndexPath(0)!], direction:.Reverse, animated: true, completion: nil)
    
  • in story board the sub pages has scroll view inside to hold the content

  • I tested subpages scroll view by calling it through segue and its working fine

Case:

  • everything work fine Only scroll view of subpages are not working at all

How to solve this issue? your guidelines will be much appreciated

Thanks,

like image 878
Engineeroholic Avatar asked Aug 08 '16 17:08

Engineeroholic


2 Answers

  1. I have created a view controller with PageViewController with three View Controllers that have scroll view in it. It works fine.

    import 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
    }
    

    }

like image 77
Saurav Avatar answered Nov 13 '22 08:11

Saurav


Similar issues are raised fairly often on Stack. For my own issue in very similar circumstances, I too was able to use the scrollview without issue in it's own view, but when used as a subview in another subview, I needed to set the scrollview dimensions programatically.

Swift 2

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

or

Swift 3

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

A further (and often viewed as a better) suggestion, automatically set the size based on contents.

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

Hope this helps.

like image 22
App Dev Guy Avatar answered Nov 13 '22 10:11

App Dev Guy