preferredContentSize
,如下所示。override func viewDidLoad() {
super.viewDidLoad()
preferredContentSize = CGSize(width: 400, height: 200)
}
When the child gets added to the parentViewController, the parentViewController gets notified about the prefered size. Following function gets called on the parent
func preferredContentSizeDidChange(forChildContentContainer container: UIContentContainer)
In this method you can then change the container to the prefered size of the child.
For example change the height to the prefered height of the child/content
override func preferredContentSizeDidChange(forChildContentContainer container: UIContentContainer) {
// heightConstraint is a IBOutlet to your NSLayoutConstraint you want to adapt to height of your content
heigtConstraint.constant = container.preferredContentSize.height
}
maybe you have to pass it through your NavigationViewController. In my example there is no NavigationViewController between the parent and the content.
initWithFrame
构造函数,创建视图的人(通常是父视图创建子视图)应该传递大小。这被称为“基于框架的布局”。这个系统有点“自上而下”。UINavigationController
。我不建议在这里使用这种布局的原因之一是导航栏通常应该在应用程序屏幕的顶部,并且在导航时不应该上下跳动。另一个原因是由UINavigationController
决定约束子视图的方式,尝试改变它的习惯并不是我推荐的做法。UINavigationController
的高度。