mercredi 6 juillet 2016

How to horizontally center a UIView inside of a UIScrollView when using autolayout?

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.

Aucun commentaire:

Enregistrer un commentaire