Why doesn't UIScrollView scroll completely after adding objects? Using storyboards, ARC, and Xcode 4.5.2

So, I know that I have similar questions, but maybe not exact ones (therefore, please do not mark me - just warn me or something like that). I was looking for days to solve this SIMPLE problem. Using storyboards, ARC, and Xcode 4.5.2, I just need to put a bunch of labels inside a UIScrollView and scroll it vertically. I tried so many combinations of content size and size settings in viewDidLoad, viewDidAppear and viewWillAppear, but to no avail. The scroll view scrolls perfectly when there is nothing in it, but when I add labels to it, the scroll only scrolls a very short section.

Note. I need to use an automatic layout, otherwise the whole project will be confused.

Here is my current code ...

.h file:

#import <UIKit/UIKit.h> @interface MortgageRatesViewController : UIViewController <UIScrollViewDelegate, UIScrollViewAccessibilityDelegate> - (IBAction)backButton:(id)sender; @property (strong, nonatomic) IBOutlet UIView *mortgageView; @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @end 

.m file:

 #import "MortgageRatesViewController.h" @interface MortgageRatesViewController () @end @implementation MortgageRatesViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } //------------------------------------------------------- - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"appBackgroundColor.png"]]; [self.scrollView setScrollEnabled:YES]; [self.scrollView setContentSize:CGSizeMake(0, 809)]; } //--------------------------------------------------------------- //--------------------------------------------------------------- //-(void)viewWillAppear:(BOOL)animated{ // // // [super viewWillAppear:animated]; // // // [self.scrollView setFrame:CGRectMake(0, 0, 320, 808)]; // // //} //--------------------------------------------------------------- -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.view addSubview:self.scrollView]; [self.scrollView setScrollEnabled:YES]; [self.scrollView setContentSize:CGSizeMake(0, 809)]; } //------------------------------------------------------------- - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)backButton:(id)sender { [self dismissViewControllerAnimated:YES completion:NO]; } @end 

Note. Having a ViewWillAppear comment doesn't matter.

EDIT: I GOT THE SOLUTION BELOW. HOPE THAT IT HELPS OTHERS!

+8
ios iphone autolayout uiscrollview storyboard
source share
3 answers

After several days of research, funny I found a solution a few minutes after posting this question! So, for my situation, I just had to add this bit of code, and it worked:

 - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self.scrollView setContentSize:CGSizeMake(320, 808)]; } 

It seems to work fine now. Please let anyone know if this is a bad way to do this because I am new to this and I would like any help. Otherwise, I will leave it and hope it can help other people! Thanks!

+39
source share

SOLUTION using Storyboard + Autolayout (without code):

An example of a vertical scroll starting at the top left corner:

  • Set top + leading constraints for the first object (subview)
  • Customize each item to the last
  • most important - set a lower limit to scroll so that it connects from top to bottom.
  • Set the scroll width - either a specific "magic number" or the best way - turn on the trailing restriction of the subtitle with the set width - if not, I usually set the label width = screen width - side paddings => standard (20) + UILabel (280) + standard (20) - this way the scrolling looks exactly at its contents width, otherwise you will get a warning of "ambiguous width".

* Requires adjustment of restrictions if horizontal scrolling is required.

+3
source share

your scroll frame is not a scrollable size.

Check contentSize

Set the border to your superview.bounds file, then set the content size to the scroolview scrollable area.

Also, I do not know that the background will scroll.

You will need to add a subview to the scrollview itself.

He has to scroll through his own subtitles. but he will not scroll himself :)

+2
source share

All Articles