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

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事件无法正常工作
答案 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)
}
}
}