设置Sub-UIViewController框架高度

时间:2014-10-09 13:37:50

标签: ios uiviewcontroller swift

我创建了一个" Horizo​​ntalSplit" - UIViewController。在那里我创建了两个UIView作为其他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)
        ...
}

之后我在该View中添加了一个新的Controller,使用了这些代码行:

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)
    }

我希望SubViewController-Frame-Size(这里称为简单的viewController)具有与之前添加的UIView-container相同的大小。 它在这里工作,因为l和o(变量)表示正确的高度。但是当我通过Horizo​​ntalViewContrller上的setTopViewController方法设置的ViewController的大小再次正确时。 当我打电话给viewDidLoad' self.view.bounds.height'在subViewController上它调用了全尺寸!而不是我之前设定的尺寸......为什么?

1 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案。感谢UIViewControllers生命周期的these post

在Sub-ViewController中,我得到viewDidLoad的高度,但在那里它实际上并没有settet帧大小。在these的帮助下,我发现viewWillAppear具有框架的高度。

希望能帮助任何人:)