如何在 swift 中将 UIViewController 添加到 UIScrollView 中?
How to add UIViewController into UIScrollView in swift?
我想将 UIViewController 添加到 UIScrollView 中。
我搜索并尝试过,但找不到解决方案。
最容易理解的方式是
这是我的代码。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.contentSize.width = self.view.frame.width * 2
let test = self.storyboard?.instantiateViewController(withIdentifier: "Test") as! TestViewController
self.addChild(test)
self.scrollView.addSubview(test.view)
test.willMove(toParent: self)
test.view.frame.origin = CGPoint.zero
}
}
但是,我这种方式是这样的错误。
如何将UIViewController添加到UIScrollView中??
请帮助我。
在您的故事板中,只需拖放一个 containerView
,就会生成一个 UIViewController
在你的 scrollView
中如你所愿
我想将 UIViewController 添加到 UIScrollView 中。
我搜索并尝试过,但找不到解决方案。
最容易理解的方式是
这是我的代码。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.contentSize.width = self.view.frame.width * 2
let test = self.storyboard?.instantiateViewController(withIdentifier: "Test") as! TestViewController
self.addChild(test)
self.scrollView.addSubview(test.view)
test.willMove(toParent: self)
test.view.frame.origin = CGPoint.zero
}
}
但是,我这种方式是这样的错误。
如何将UIViewController添加到UIScrollView中??
请帮助我。
在您的故事板中,只需拖放一个 containerView
,就会生成一个 UIViewController
在你的 scrollView
中如你所愿