Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't UIScrollView scroll fully after adding objects? Using storyboard, ARC, and Xcode 4.5.2

So, I know there are similar questions to mine, but maybe not exact (so please don't mark me down -- just warn me or something). I have searched for days for the solution to this SIMPLE issue. Using storyboards, ARC, and Xcode 4.5.2, I simply need to put a bunch of labels inside a UIScrollView and have it scroll vertically. I've tried so many combinations of setting frame sizes and content sizes within viewDidLoad, viewDidAppear, and viewWillAppear, but to no avail. The scroll view scrolls perfectly when there's nothing inside of it, but when I add labels to it, the scrolling only scrolls a very short section.

Note: I need to use auto layout, otherwise my whole project will get messed up.

Here's 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 viewWillAppear commented out has made no difference.

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

like image 417
LargeGlasses Avatar asked Dec 29 '12 00:12

LargeGlasses


1 Answers

After days of research, it's funny I actually found the solution just minutes after posting this question! So, for my situation I simply had to add this bit of code, and it worked:

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

It seems to be working perfectly now. Please, anyone, let me know if this is a poor way of doing it, because I'm new to this and I would love any help. Otherwise, I'll leave it and hope this can help other people! Thanks!

like image 108
LargeGlasses Avatar answered Sep 19 '22 12:09

LargeGlasses