子视图滚动不起作用 SWIFT

Posted

技术标签:

【中文标题】子视图滚动不起作用 SWIFT【英文标题】:subview scroll is not working SWIFT 【发布时间】:2016-08-08 17:54:48 【问题描述】:

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

详情:

我制作了一个 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函数,通过restore Identifiers添加页面

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

EditTabs.setViewControllers([pagesAtIndexPath(0)!], direction:.Reverse, animated: true, completion: nil)

在故事板中,子页面内部有滚动视图来保存内容

我通过 segue 调用它来测试子页面滚动视图,它的工作正常

案例:

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

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

谢谢,

【问题讨论】:

【参考方案1】:

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

    导入 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
    
    

【讨论】:

【参考方案2】:

Stack 上经常出现类似的问题。对于我自己在非常相似的情况下的问题,我也能够在它自己的视图中毫无问题地使用滚动视图,但是当用作另一个子视图中的子视图时,我需要以编程方式设置滚动视图尺寸。

斯威夫特 2

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

斯威夫特 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 

希望这会有所帮助。

【讨论】:

以上是关于子视图滚动不起作用 SWIFT的主要内容,如果未能解决你的问题,请参考以下文章

滚动视图不起作用iOS Swift

Swift - UIScrollView 滚动动画在 viewDidLoad() 中不起作用

Swift 3:按钮在控制器上不起作用添加为子视图

当滚动视图子视图不在屏幕上时,手势识别器不起作用

尝试添加为子视图时,ScrollView 不起作用

Swift编程约束下边距不起作用