gpt4 book ai didi

ios - subview 滚动不起作用 SWIFT

转载 作者:可可西里 更新时间:2023-11-01 03:10:58 25 4
gpt4 key购买 nike

目标:让一个viewcontroller有多个页面并且可以通过分段 Controller 进行交换,页面内容可以垂直滚动

详情:

  • 我制作了一个 pagviewcontroller 并将其作为 subview 嵌入到主 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)
    }
  • 我调用了 pageViewController 函数,我通过恢复标识符添加页面

  • 我通过获取 pageindex 和设置 viewcontroller 来管理分段 View Controller ,如下所示:

    EditTabs.setViewControllers([pagesAtIndexPath(0)!], direction:.Reverse, animated: true, completion: nil)
  • 在 Storyboard中,子页面内部有 ScrollView 来保存内容

  • 我通过 segue 调用它来测试子页面 ScrollView ,它工作正常

案例:

  • 一切正常 只有子页面的 ScrollView 根本不起作用

如何解决这个问题?您的指南将不胜感激

谢谢,

最佳答案

  1. 我使用 PageViewController 创建了一个 View Controller ,其中包含三个具有 ScrollView 的 View Controller 。它工作正常。

    导入 UIKit类 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
    }

关于ios - subview 滚动不起作用 SWIFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38835618/

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