1

I have 3 views that I'm loading into a scroll view so users can swipe through the app to change views (similar to Snapchat or Tinder navigation). However, I added a horizontally and vertically centered label to one of the nibs and it appears as though the size is much wider than my screen. I have the nib frames set to the size of the device screen.

How can I make the subviews match the frame of the view (full screen)?

Here's what I've got in the viewDidLoad for the view controller with a full screen scroll view in it, as well of a screenshot of the result:

@IBOutlet weak var scrollViewOfViews: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    let settingsVC = UIViewController(nibName: "SettingsViewController", bundle: nil)
    let friendsFeedVC = UIViewController(nibName: "FriendsFeedViewController", bundle: nil)
    let notificationsVC = UIViewController(nibName: "NotificationsViewController", bundle: nil)

    let screenSize = UIScreen.mainScreen().bounds
    settingsVC.view.frame = screenSize
    friendsFeedVC.view.frame = screenSize
    notificationsVC.view.frame = screenSize

    self.addChildViewController(settingsVC)
    self.scrollViewOfViews.addSubview(settingsVC.view)
    settingsVC.didMoveToParentViewController(self)

    self.addChildViewController(friendsFeedVC)
    self.scrollViewOfViews.addSubview(friendsFeedVC.view)
    friendsFeedVC.didMoveToParentViewController(self)

    self.addChildViewController(notificationsVC)
    self.scrollViewOfViews.addSubview(notificationsVC.view)
    notificationsVC.didMoveToParentViewController(self)

    var friendsFeedFrame = friendsFeedVC.view.frame
    friendsFeedFrame.origin.x = self.view.frame.width
    friendsFeedVC.view.frame = friendsFeedFrame

    var notificationsFrame = notificationsVC.view.frame
    notificationsFrame.origin.x = self.view.frame.width * 2
    notificationsVC.view.frame = notificationsFrame

    self.scrollViewOfViews.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.size.height)

    // start scrollview page on middle view
    var startFrame = scrollViewOfViews.frame
    startFrame.origin.x = self.view.frame.width
    startFrame.origin.y = 0
    scrollViewOfViews.scrollRectToVisible(startFrame, animated: false)

}

The result for the friendsFeedVC with a centered Label: enter image description here

cb428
  • 485
  • 1
  • 6
  • 19

1 Answers1

0

"You should not initialize UI geometry-related things in viewDidLoad, because the geometry of your view is not set at this point and the results will be unpredictable"

"viewWillAppear: is the correct place to do these things, at this point the geometry is set so getting and setting geometry-related properties makes sense."

Or you can set them in viewDidLayoutSubviews

Try to place the codes that set frame of views in viewWillAppear or viewDidLayoutSubviews

Ref: Why am I having to manually set my view's frame in viewDidLoad?

Community
  • 1
  • 1
tuledev
  • 10,177
  • 4
  • 29
  • 49
  • I moved the code where I make the frames the screenSize to `viewWillAppear` and `viewDidLayoutSubviews` but it's not changing the outcome. I even moved all of the viewDidLoad code there and nothing changed. – cb428 Dec 26 '15 at 17:49
  • Please try to change the background color for debugging. I can not see what view is? – tuledev Dec 26 '15 at 17:52
  • You mean the green snapshot I took? That's the view background, the other two views to the left and right are different colors. – cb428 Dec 26 '15 at 18:03
  • "How can I make the subviews match the frame of the view (full screen)?" I mean, you can try to change the background of `friendsFeedVC` to see where actually it is. And try to change its frame in `viewDidLayoutSubviews`, e.g change to (100,100,100,100), ... To debug that the `setFrame` work or not. – tuledev Dec 26 '15 at 18:11