UIScrollView not scrolling

It’s always good to show a complete working code snippet: // in viewDidLoad (if using Autolayout check note below): UIScrollView *myScrollView; UIView *contentView; // scrollview won’t scroll unless content size explicitly set [myScrollView addSubview:contentView];//if the contentView is not already inside your scrollview in your xib/StoryBoard doc myScrollView.contentSize = contentView.frame.size; //sets ScrollView content size Swift 4.0 … Read more

How do I auto size a UIScrollView to fit its content

The best method I’ve ever come across to update the content size of a UIScrollView based on its contained subviews: Objective-C CGRect contentRect = CGRectZero; for (UIView *view in self.scrollView.subviews) { contentRect = CGRectUnion(contentRect, view.frame); } self.scrollView.contentSize = contentRect.size; Swift let contentRect: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in rect = rect.union(view.frame) } scrollView.contentSize … Read more

Center content of UIScrollView when smaller

I’ve got very simple solution! All you need is to update the center of your subview (imageview) while zooming in the ScrollViewDelegate. If zoomed image is smaller than scrollview then adjust subview.center else center is (0,0). – (void)scrollViewDidZoom:(UIScrollView *)scrollView { UIView *subView = [scrollView.subviews objectAtIndex:0]; CGFloat offsetX = MAX((scrollView.bounds.size.width – scrollView.contentSize.width) * 0.5, 0.0); CGFloat … Read more

What’s the UIScrollView contentInset property for?

It sets the distance of the inset between the content view and the enclosing scroll view. Obj-C aScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 7.0); Swift 5.0 aScrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 7.0) Here’s a good iOS Reference Library article on scroll views that has an informative screenshot (fig 1-3) – I’ll replicate … Read more

How to detect when a UIScrollView has finished scrolling

The 320 implementations are so much better – here is a patch to get consistent start/ends of the scroll. -(void)scrollViewDidScroll:(UIScrollView *)sender { [NSObject cancelPreviousPerformRequestsWithTarget:self]; //ensure that the end of scroll is fired. [self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:sender afterDelay:0.3]; … } -(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { [NSObject cancelPreviousPerformRequestsWithTarget:self]; … }

Programmatically scroll a UIScrollView

You can scroll to some point in a scroll view with one of the following statements in Objective-C [scrollView setContentOffset:CGPointMake(x, y) animated:YES]; or Swift scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true) See the guide “Scrolling the Scroll View Content” from Apple as well. To do slideshows with UIScrollView, you arrange all images in the scroll view, … Read more

How to add leading padding to view added inside an UIStackView

When isLayoutMarginsRelativeArrangement property is true, the stack view will layout its arranged views relative to its layout margins. stackView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20) stackView.isLayoutMarginsRelativeArrangement = true But it affects all arranged views inside to the stack view. If you want this padding for only one arranged view, you need to … Read more