【问题标题】:Set Sub-UIViewController frames height设置 Sub-UIViewController 框架高度
【发布时间】:2014-10-09 13:37:49
【问题描述】:

我创建了一个“Horizo​​ntalSplit”-UIViewController。我创建了两个UIViews 作为其他 UIViewController 的容器。 Containers 的高度是屏幕的一半。

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

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

【问题讨论】:

    标签: ios uiviewcontroller swift


    【解决方案1】:

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

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

    希望对任何人也有帮助:)

    【讨论】:

    • 赞成,因为我一直在寻找关于 UIViewController 生命周期的帖子! :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多