ScrollView宽度的Autolayout问题(错误的值)

时间:2014-07-02 08:07:46

标签: storyboard swift

我正在使用xCode 6 Storyboard中的autolayout。

问题:所有元素的位置都是正确的。但我的ViewController得到了错误的scrollView宽度。在iPad上我得到300(用于电话)而不是500。

出了什么问题

func setupView() {
    let pagesScrollViewSize :CGSize = self.scrollView.frame.size;

    var vc : IntroPageViewController!
    var vc2 : IntroPageViewController!

    if(isPad()) {
        println("is Pad. width: \(self.scrollView)")  // shows width: 300. Has to be 500
        vc = self.storyboard.instantiateViewControllerWithIdentifier(AppConfiguration.MainStoryboard.ViewControllerIdentifiers.InfoPage1_Pad) as IntroPageViewController
        vc2 = self.storyboard.instantiateViewControllerWithIdentifier(AppConfiguration.MainStoryboard.ViewControllerIdentifiers.InfoPage2_Pad) as IntroPageViewController
    } else {
        println("is phone")
        vc = self.storyboard.instantiateViewControllerWithIdentifier(AppConfiguration.MainStoryboard.ViewControllerIdentifiers.InfoPage1_Phone) as IntroPageViewController
        vc2 = self.storyboard.instantiateViewControllerWithIdentifier(AppConfiguration.MainStoryboard.ViewControllerIdentifiers.InfoPage2_Phone) as IntroPageViewController

    }

    self.scrollView.addSubview(vc.view)
    vc2.view.setX(self.scrollView.width())
    self.scrollView.addSubview(vc2.view)

    // close button listener
    let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "closeIntro:")
    vc2.closeButton.addGestureRecognizer(tap)

    self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * 2, pagesScrollViewSize.height)

}

iPad View

iPhone View

1 个答案:

答案 0 :(得分:1)

我找到了答案: 您必须在viewDidLayoutSubviews()方法中运行此代码。

iOS AutoLayout - get frame size width