childViewControllerを使用してプロジェクトのビューを区切りますが、奇妙な問題が発生しました。ここに私のコードがあります。childViewController.view.frameのUIViewに異なる値があります
class ViewController: UIViewController {
var container = UIView()
var childVC = ChildViewController()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
addChildViewController(childVC)
childVC.didMove(toParentViewController: self)
addChildView()
setContainerFrame()
}
func setContainerFrame() {
container.frame = CGRect(x: 0, y: 100, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 100)
container.backgroundColor = .red
view.addSubview(container)
}
func addChildView() {
let frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
childVC.view.frame = frame
childVC.view.backgroundColor = .green
container.addSubview(childVC.view)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("childVC.view.frame: \(childVC.view.frame)")
}
}
class ChildViewController: UIViewController {
}
IがのViewController者のviewDidLoad()、xcode'console印刷異なるフレームログに)((FUNCのaddChildViewを呼び出す)およびFUNCのsetContainerFrame順序を交換します。
'viewDidLayoutSubviews()'で試してください –