0

I'm using a UIScrollView to swipe horizontally between two ViewControllers. On iPhone 6 and higher the VCs fit perfectly in the screen. However, when testing on an iPhone 5, the second VC isn't displayed entirely. The scrollView will not display all of "Page 2" (the right VC), but will fit the left VC perfectly. I have the scrollView's contentSize width set to equal 2 times the screen's width (since there are two full screen VCs).

let screenRect = UIScreen.main.bounds

var adminFrame : CGRect = pageTwo.view.frame;
adminFrame.origin.x = adminFrame.width;
pageTwo.view.frame = adminFrame;

var BFrame : CGRect = pageOne.view.frame;
BFrame.origin.x = 2 * screenRect.width;
self.scrollView.contentOffset.x = BFrame.width

self.scrollView.contentSize = CGSize(width: screenRect.width * 2, height: screenRect.height)

If I arbitrarily add to the contentSize width, then it does fit better, but is not a perfect fit nor does it seem like a good solution.

Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
  • Where is that code located? – matt Jan 21 '17 at 17:18
  • Good question, it's located within the base VC, which is also a subclass of UIScrollViewDelegate, in the viewDidLoad() method. – user7367181 Jan 21 '17 at 17:26
  • So that is likely to be a lot of your problem right there. In `viewDidLoad`, things do not have their real sizes yet. So your answer to queries like `adminFrame = pageTwo.view.frame` are likely to be wrong. – matt Jan 21 '17 at 17:51
  • @matt That makes perfect sense, what method would you recommend to use that would work for this? – user7367181 Jan 21 '17 at 18:06
  • If my theory is right, you would need to "wait" until at least after `viewDidLayoutSubviews` has been called for the first time. But watch out because it can be called many times thereafter. So if you use `viewDidLayoutSubviews`, make sure you don't run the same code multiple times. – matt Jan 21 '17 at 18:11
  • @matt Do you have any suggestions on how to ensure the code is only called once within `viewDidLayoutSubviews`? Thank you – user7367181 Jan 21 '17 at 18:55
  • 1
    suggestion: If you need to swipe between ViewControllers the us "UIPageViewController" It is loads better and swipe between controller is handled by default. – Abdul Rehman Jan 21 '17 at 20:05
  • 1
    Yes, actually I agree with @AbdulRehmanWarraich - see my answer here: http://stackoverflow.com/a/41710711/341994 – matt Jan 21 '17 at 21:06
  • For "only called once", see my answer here: http://stackoverflow.com/a/37287923/341994 – matt Jan 21 '17 at 21:07

0 Answers0