This should be a VERY easy problem but I'm having trouble getting the desired result. I have horizontally scrolling UIScrollView with a width of 320. The width of its content is 5000. I'm trying to center the content with:
// 'self' is the UIScrollView CGFloat newContentOffsetX = (self.width/2) + (self.contentSize.width/2); self.contentOffset = CGPointMake(newContentOffsetX, 0);
I don't understand why this is not centering the content. Can you see something wrong with the calculations?
The scroll view displays its content within the scrollable content region. As the user performs platform-appropriate scroll gestures, the scroll view adjusts what portion of the underlying content is visible. ScrollView can scroll horizontally, vertically, or both, but does not provide zooming functionality.
I think you want:
CGFloat newContentOffsetX = (self.contentSize.width/2) - (self.bounds.size.width/2);
diagram:
|------------------------self.contentSize.width------------------------| |-----self.width-----| |-------- offset --------| ^ center
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With