UIScrollView setContentSize not working

I have a really big problem with UIScrollView. To make sure that this is not my actual project, I created a new project, only for iPhone, for iOS 6 and 7. I have auto-detection disabled .

  • I created a tab bar project
  • I created a view controller, embedded it in the navigation controller, and connected it as a tab bar item # 0
  • I put a UIScrollView in my opinion.
  • I have enabled scrolling in a custom UIViewController created for this view.
  • I synthesized my IBOutlet and assigned a delegate to myself. I am also an implemented delegate in .h

Here is my code (ViewDidLoad):

self.scrollView.delegate = self; [self.scrollView setScrollEnabled:YES]; [self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)]; NSLog(@"contentSize width %f", self.scrollView.contentSize.width); NSLog(@"contentSize height %f", self.scrollView.contentSize.height); NSLog(@"%@", NSStringFromCGAffineTransform(self.scrollView.transform)); 

It returns me "contentSize width 20000.000000", "contentSize height 0.000000" and "[1, 0, 0, 1, 0, 0]". It scrolls left-right, where it should scroll up and down

Please, help!

+9
ios objective-c iphone autolayout uiscrollview
source share
5 answers

I had the same problem until I realized that I did not install the UIScrollView delegate. Also, if you use auto-layout, you need to wait for the layout to be applied to the UIScrollView. This means that you must set the content size to "viewDidLayoutSubviews".

+39
source share

In the viewDidLoad method, the frame may be incorrect; move the code to this method:

 - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; // Your code } 
+6
source share

Use this code. ScrollView setContentSize should be called asynchronous in the main thread.

Swift:

 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() DispatchQueue.main.async { var contentRect = CGRect.zero for view in self.scrollView.subviews { contentRect = contentRect.union(view.frame) } self.scrollView.contentSize = contentRect.size } } 

Goal C:

  - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; dispatch_async(dispatch_get_main_queue(), ^ { CGRect contentRect = CGRectZero; for(UIView *view in scrollView.subviews) contentRect = CGRectUnion(contentRect,view.frame); scrollView.contentSize = contentRect.size; }); } 
+4
source share

Well, there is nothing wrong with the code. So, some suggestions:

  • Make sure you add to your .h file like this:

     @interface yourViewController : UIViewController <UIScrollViewDelegate> { } 
  • Try moving this piece of code to the viewWillLoad method:

     - (void)viewWillAppear:(BOOL)animated { self.scrollView.delegate = self; [self.scrollView setScrollEnabled:YES]; [self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)]; } 
+3
source share
 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() var contentRect = CGRect.zero for view: UIView in scrollView.subviews { if !view.isHidden { contentRect = contentRect.union(view.frame) } } scrollView.contentSize = contentRect.size scrollView.contentSize.width = self.view.frame.width } 

Swift 3.X working code Converted @ Karen Hovhannisyan answer.

0
source share

All Articles