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

前端之家收集整理的这篇文章主要介绍了ios – 子视图滚动不能正常工作SWIFT前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
目标:使视图控制器具有多个页面并且可以通过分段控制器进行交换,页面内容可以垂直滚动

细节:

>我制作了一个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,completion: nil)

>在故事板中,子页面内部具有滚动视图以保存内容
>我测试了子页面滚动视图,通过segue调用它并且工作正常

案件:

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

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

谢谢,

解决方法

>我创建了一个带有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,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
}

}

原文链接:/iOS/334205.html

猜你在找的iOS相关文章