ios - 如何快速管理嵌套 View Controller View 大小?

标签 ios swift

我有一个父 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/

相关文章:

ios - objc 与协议(protocol)继承不兼容的指针类型

c# - 使用 Xamarin 从 iPhone 中的麦克风流式传输音频

ios - 具有多个目标的恼人的 XCode Source Kit 错误

ios - 在 Realm 中进行大规模更新的首选方法?

ios - 如何呈现 UIVideoEditorController

ios - Obj-C - Xcode 控制台中的 TIC 读取状态?

ios - Xcode:架构 x86_64 的 undefined symbol : "_OBJC_CLASS_$_AVPlayer"

ios - 禁用多个按钮的简单方法?

swift - 在 NSEventMask 中匹配多个 NSEvents

swift - 在变量中分配运算符函数