我有一个父 View Controller ,想在其中构建一个嵌套的 View Controller 。父 View Controller 在屏幕中间有一个 contentView(一个 UIView)。单击按钮时,我希望嵌套 View Controller 出现在 contentView 位置。但是当我单击按钮时,嵌套 View 将显示在 contentView 之外。下面是我正在使用的代码。 currentViewController 是嵌套 View 。 contentView 是父 View 。我发现的有趣的事情是,如果我使用 UITableViewController 作为嵌套 Controller ,一切正常。嵌套 View 正确位于其父 View 内。当我使用 UIViewController 时它失败了。我无法弄清楚出了什么问题。
self.currentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("NestedViewController")
self.addChildViewController(self.currentViewController!)
self.currentViewController?.view.translatesAutoresizingMaskIntoConstraints = false
self.currentViewController?.view.frame = self.contentView.bounds
self.contentView.addSubview((self.currentViewController?.view)!)
self.currentViewController?.didMoveToParentViewController(self)
self.currentViewController?.view.layoutIfNeeded()
最佳答案
你应该删除那行
self.currentViewController?.view.translatesAutoresizingMaskIntoConstraints = false
因为当您想使用自动布局约束时会用到它。
关于ios - 如何快速管理嵌套 View Controller View 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35824413/