[英]Multiple child view controllers inside a uicontainerview (container view)
[英]Resizing Child View Controllers in UIContainerView swift ios
我正在创建一个应用程序,我必须在视图控制器中打开2-3个视图控制器,因为我想为这些视图控制器共享相同的滑动抽屉和导航栏。 我已经按照本教程。
我在MainController中使用了ContainerView ,并且正确添加了子控制器但是我很难调整子控制器的大小以匹配containerview
self.mainContainer.addSubview(vc.view)
self.mainContainer.translatesAutoresizingMaskIntoConstraints = false
addChildViewController(vc)
NSLayoutConstraint.activate([
vc.view.leadingAnchor.constraint(equalTo:mainContainer.leadingAnchor),
vc.view.trailingAnchor.constraint(equalTo: mainContainer.trailingAnchor),
vc.view.topAnchor.constraint(equalTo: mainContainer.topAnchor),
vc.view.bottomAnchor.constraint(equalTo: mainContainer.bottomAnchor)
])
vc.didMove(toParentViewController: self)
我收到以下错误
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x170095c70 h=-&- v=-&- UIView:0x127e17630.midY == UIView:0x127e15070.midY + 32 (active)>",
"<NSLayoutConstraint:0x1740970c0 V:|-(0)-[UIView:0x127e17630] (active, names: '|':UIView:0x127e15070 )>",
"<NSLayoutConstraint:0x174097020 UIView:0x127e17630.bottom == UIView:0x127e15070.bottom (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x174097020 UIView:0x127e17630.bottom == UIView:0x127e15070.bottom (active)>
我认为是self.mainContainer.translatesAutoresizingMaskIntoConstraints = false以某种方式不起作用子视图控制器没有正确调整大小,因为在主控制器中导航栏大约是64 ,并且相同高度的部分从底部被截取到子视图控制器中。
MainController的元素约束
navbar left = top = right = 0,height = 64
UIContainerView left = right = bottom = 0并且top to navbar = 0
您可以简单地设置vc
的框架,即。而不是将约束应用于vc's
view
vc.view.frame = self.containerView.bounds //Here
self.containerView.addSubview(vc.view)
self.addChildViewController(vc)
vc.didMove(toParentViewController: self)
vc.view.translatesAutoresizingMaskIntoConstraints = false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.