Swift 4:scrollViewDidScroll无效

时间:2017-11-30 08:33:17

标签: swift swift4

  1. 我创建了第一个View控制器" MainViewController"并使用插座 contentContainer
  2. 将容器视图放入其中
  3. 我创建了第二个View Controller" AboutContentViewController"并将其放入ScrollView
  4. 我以编程方式连接Container View和AboutContentViewController:
  5. 
    
    class MainViewController: UIViewController {
        @IBOutlet weak var contentContainer: UIView!
    
        override func viewDidLoad() {
            let contentControllerName = "AboutContentViewController"
            if let contentController = storyboard?.instantiateViewController(withIdentifier: contentControllerName) {
                contentContainer.addSubview(contentController.view)
            }
        }
    }
    
    
    

    1. 我尝试使用此代码
    2. 捕获scrollViewDidScroll事件

      
      
      class AboutContentViewController: UIViewController, UIScrollViewDelegate {
          @IBOutlet weak var topGalleryScrollView: UIScrollView!
             
          override func viewDidLoad() {
              super.viewDidLoad()
              
              topGalleryScrollView.contentSize = CGSize(width: 3750.0, height: 417.0)
              topGalleryScrollView.delegate = self
          }
          
          func scrollViewDidScroll(_ scrollView: UIScrollView) {
              print("scrollViewDidScroll called")
          }
      }
      
      
      

      在此上下文中,scrollViewDidScroll事件无法正常工作

1 个答案:

答案 0 :(得分:0)

我已经解决了问题。在将其视图添加到容器之前,需要将AboutContentViewController作为子视图控制器添加到MainVewController:

class MainViewController: UIViewController {
    @IBOutlet weak var contentContainer: UIView!

    override func viewDidLoad() {
        let contentControllerName = "AboutContentViewController"
        if let contentController = storyboard?.instantiateViewController(withIdentifier: contentControllerName) {
            addChildViewController(contentController)
            contentContainer.addSubview(contentController.view)
        }
    }
}