Ios 设置子UIViewController帧高度
我已经创建了一个“水平拆分”-Ios 设置子UIViewController帧高度,ios,uiviewcontroller,swift,Ios,Uiviewcontroller,Swift,我已经创建了一个“水平拆分”-UIViewController。在这里,我创建了两个UIViews作为其他UIViewController的容器。容器的高度为屏幕的一半 override func viewDidLoad() { super.viewDidLoad() ... self.topViewContainer = UIView(frame: CGRectMake(0, 0, self.view.bounds.width, complete
UIViewController
。在这里,我创建了两个UIView
s作为其他UIViewController的容器。容器的高度为屏幕的一半
override func viewDidLoad() {
super.viewDidLoad()
...
self.topViewContainer = UIView(frame: CGRectMake(0, 0, self.view.bounds.width, completeHeight / 2))
self.topViewContainer.backgroundColor = UIColor.orangeColor()
self.view.addSubview(topViewContainer)
self.bottomViewContainer = UIView(frame: CGRectMake(0, completeHeight / 2, self.view.bounds.width, completeHeight / 2))
self.bottomViewContainer.backgroundColor = UIColor.blueColor()
self.view.addSubview(bottomViewContainer)
...
}
之后,我在该视图中放置了一个新控制器,并使用了以下代码行:
func setTopViewController(viewController: UIViewController) {
if self.topViewController != nil {
self.topViewController.removeFromParentViewController()
self.topViewController.view.removeFromSuperview()
}
var l = topViewContainer.frame.height
self.topViewController = viewController
self.topViewController.view.frame = CGRect(x: 0, y: 0, width: topViewContainer.bounds.width, height: topViewContainer.bounds.height)
var o = topViewController.view.frame.height
self.addChildViewController(self.topViewController)
self.topViewContainer.addSubview(self.topViewController.view)
self.topViewController.didMoveToParentViewController(self)
}
我希望子viewController帧大小(这里称为simple viewController)与我之前添加的UIView容器大小相同。
它在这里起作用,因为l和o(变量)表示正确的高度。但是当我通过水平视图控制器上的setTopViewController方法设置ViewController时,大小不再正确。
当我在subViewController上调用viewDidLoad“self.view.bounds.height”时,它调用了全尺寸!而不是我以前设定的尺寸。。。为什么? 好的,我找到了解决办法。得益于UIViewController的生命周期: 在子ViewController中,我得到了
viewDidLoad
中的高度,但在那里,它实际上没有设置帧大小。另外,在的帮助下,我发现视图将出现
具有框架的settet高度
希望这对任何人都有帮助:)投票通过,因为我一直在寻找关于UIViewController生命周期的帖子!:-)