0

i am struggling with autolayout in xCode 6 Storyboard.

The Problem: All elements have the correct size an position. But my ViewController get the wrong width of the scrollView. On iPad I get 300 (which is for Phone) instead of 500.

What's wrong

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

GoGreen
  • 2,251
  • 1
  • 17
  • 29
fabian
  • 5,433
  • 10
  • 61
  • 92
  • One (maybe) unrelated observation: you shouldn't set the `contentSize` of your scroll view explicitly. You should do this by adding constraints for its subviews. See my answer [here](http://stackoverflow.com/questions/23947094/scrollview-doesnt-work-even-though-contentsize-is-larger-than-frame-and-subview/23947444#23947444). – Levi Jul 02 '14 at 08:17

1 Answers1

1

I found the answer: You have to run this code in the viewDidLayoutSubviews() method.

iOS AutoLayout - get frame size width

Community
  • 1
  • 1
fabian
  • 5,433
  • 10
  • 61
  • 92