嵌入为子视图控制器时,UINavigationController的高度为零

时间:2018-07-13 08:34:04

标签: ios swift uinavigationcontroller autolayout

我有一个应用,其中包含孩子UIView的{​​{1}}从底部向上滑动。 子控制器通过自动布局来定义高度。

这是我的工作代码:

UIViewController

现在,我想在动画之前将现有的子控制器嵌入// controller is new added controller addChildViewController(controller) controller.view.translatesAutoresizingMaskIntoConstraints = false bottomSheetViewContainerView.translatesAutoresizingMaskIntoConstraints = false bottomSheetViewContainerView.addSubview(controller.view) // Setting only top, left, right anchors so the view isn't squished on sliding up controller.view.leftAnchor.constraint(equalTo: bottomSheetViewContainerView.leftAnchor).isActive = true controller.view.rightAnchor.constraint(equalTo: bottomSheetViewContainerView.rightAnchor).isActive = true controller.view.topAnchor.constraint(equalTo: bottomSheetViewContainerView.topAnchor).isActive = true // This calculates the correct height bottomSheetViewContainerView.layoutIfNeeded() print("controller.view.bounds.height: \(controller.view.bounds.height)") bottomSheetViewContainerView.heightAnchor.constraint(equalToConstant: controller.view.bounds.height).isActive = true // bottomSheetContainerView slides up UIView.animate(withDuration: 0.40) { self.view.layoutIfNeeded() } 中。但是当我这样做时,高度为零。不知何故,它没有达到顶部ViewController的高度...

UINavigationController的高度也为零。如何强制计算其高度?

1 个答案:

答案 0 :(得分:0)

///仅设置顶部,左侧,右侧锚点,以使视图在向上滑动时不会被压缩

controller.view.leftAnchor.constraint(equalTo: bottomSheetViewContainerView.leftAnchor).isActive = true
controller.view.rightAnchor.constraint(equalTo: bottomSheetViewContainerView.rightAnchor).isActive = true
controller.view.topAnchor.constraint(equalTo: bottomSheetViewContainerView.topAnchor).isActive = true

您应该添加底部> = 0约束

然后删除

bottomSheetViewContainerView.heightAnchor.constraint(equalToConstant: controller.view.bounds.height).isActive = true

如果控制器视图中的约束正确无误,那么它应该起作用。