1

I have a UIView that is placed inside of a scrollView. The UIView has 4 constraints that I have set in storyboard:

  • Equal Width To scrollView (w/ a 0.98 multiplier)
  • Equal Height To scrollView (w/ a 0.98 multiplier)
  • Horizontally Centered inside scrollView (w/ constant = 0)
  • Vertically Centered inside scrollView (w/ constant = 0)

The scrollView has the regularly trailing, leading, top, & bottom constraints with all constants = 0.

I have also set the following property for my scrollView.

self.scrollView.contentSize.width = self.scrollView.frame.size.width + 30

How could I center the UIView inside of my scrollView?

Note: I do have access to a self.horizontalCenterConstraint.constant, which can control where the UIView is located inside the scrollView. I tried doing

self.horizontalCenterConstraint.constant = (self.scrollView.contentSize.width / 2)

but this does not actually center the UIView inside the scrollView.

AppreciateIt
  • 696
  • 2
  • 8
  • 23
  • in which method do you set the `contentSize` and `horizontalCenterConstraint`? (viewDidLoad, viewWillAppera...) – tbilopavlovic Jun 14 '16 at 22:19
  • This all occurs inside of a UITableViewCell, so I made the cell override traitCollectionDidChange(..), and that is where I set them. Also in awakeFromNib(..) initially. – AppreciateIt Jun 14 '16 at 22:21
  • I think you need to add trailing, leading, top and bottom between view and scrollView, so scrollview can calculate how far view is from contentSize margins – tbilopavlovic Jun 14 '16 at 22:27
  • Why do you use autolayout constraints and set scrollView.contentSize at the same time? You should either rely on autolayout OR manually set contentSize. https://developer.apple.com/library/ios/technotes/tn2154/_index.html – Fyodor Volchyok Jun 14 '16 at 22:44
  • and if you want your content to be 30 point wider than scrollView you could set it in constraint property named `constant`. In your case it's value is 30. When you solve issues with constraints autolayout should do the rest for you, no code needed. – Fyodor Volchyok Jun 14 '16 at 22:49
  • @FyodorVolchyok How about handling contentOffset? See my comment below to understand what I mean. – AppreciateIt Jun 15 '16 at 00:30
  • Actually this is the problem I now see: http://stackoverflow.com/questions/31844493/autolayout-is-changing-uiscrollviews-contentoffset-on-rotation – AppreciateIt Jun 15 '16 at 00:44

1 Answers1

1

I suggest to add containerView as the root subview of scrollView (leading, trailing, top, bottom = 0 and equal width and height as scrollView) and then add this view that you want.

  • scrollView
    • containerView
      • YOUR_VIEW (same constraints as you already have)

Now scrollView can calculate the contentSize because of constraints of containerView, and i see that you added self.scrollView.contentSize.width = self.scrollView.frame.size.width + 30 so just set width of containerView wider than scrollView and it will scroll horizontally....

tbilopavlovic
  • 1,096
  • 7
  • 13
  • 1
    This works, there was one modification I made to make it work though. I had to set the horizontalCenterConstraint.constant to be half of what the EqualWidths.constant was at. So for instance, if the scrollView had to have 30 points on each side of my UIView, then I would have the EqualWidths.constant = 30 and the horizontalCenterConstraint.constant = 15. The contentOffset of the scrollView also has to be modified, but my scrollView isn't allowing it (there are no visible changes). Maybe there is a misunderstanding? – AppreciateIt Jun 14 '16 at 23:39
  • Modifying the scrollView.contentoffset manually is quite annoying, especially since it only appears to be working when I subclass the scrollView and do it in override layoutSubviews(..). It also has to be modified whenever there is a traitCollection change, since the offset changes every time the device rotates. – AppreciateIt Jun 15 '16 at 00:36