【问题标题】:Autolayout issues with ScrollView width (wrong value)ScrollView 宽度的自动布局问题(错误值)
【发布时间】:2014-07-02 08:07:46
【问题描述】:

我在 xCode 6 Storyboard 中遇到自动布局问题。

问题:所有元素都有正确的大小和位置。但是我的 ViewController 得到了错误的滚动视图宽度。在 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)

}

【问题讨论】:

  • 一个(可能)不相关的观察:您不应该明确设置滚动视图的contentSize。您应该通过为其子视图添加约束来做到这一点。看我的回答here

标签: storyboard swift


【解决方案1】:

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

iOS AutoLayout - get frame size width

【讨论】:

    猜你喜欢
    • 2020-01-14
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 2010-12-04
    相关资源
    最近更新 更多