Why won't UIScrollView scroll fully after addi

2019-02-03 21:03发布

问题:

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!

回答1:

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!



回答2:

SOLUTION with Storyboard + Autolayout (no code):

Vertical scrolling example that starts from top-left corner:

  • Set the top+leading constraints of the first object (subview)
  • Chain up every object till the last one
  • The most important - set the bottom constraint to the scroll view so it's hooked up top to bottom.
  • Set the width of the scroll view - either a specific "magic number" or a better way - connect the trailing constraint of a subview that has a set width - if there's non I usually set a label's width = screen width - side paddings => standard(20)+UILabel(280)+standard(20) - this way the scroll view knows exactly its content's width, otherwise you'll get an "ambiguous width" warning.

*Needs constraints' adjustments if horizontal scrolling is desired.



回答3:

your scrollview's frame is not the scrollable size.

Check out contentSize

Set frame to its superview.bounds then set content size to the scroolview's scrollable area.

Also I dont know that the background will scroll.

You will want to add subview's to the scrollview itself.

It should scroll its own subviews. but it will not scroll itself :)