Question

I have been trying to implement scroll view all day and watched as many as 30 tutorials (ok not that many but enough) and many text tutorials later but still won't work as everyone has different methods... I'm going insane here, can someone help? Here is the line of code that apparently works with scroll view... plus the steps I've taken:

created my second controller (named it View Controller2), dragged scroll view(named scroller) onto it and then started coding.

.h:

@interface ViewController2 : UIViewController {

    IBOutlet UIScrollView *scroller;

}
@end

.m:

@implementation ViewController2
{
    [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 600)];
    [super viewDidLoad];
}

@end

I then after dragged the scroller from the attributes onto the UI scroll View and when I try running it I get errors with regards to the .m code? I also put two buttons on the view controller one at the top and another at the bottom to see if it would scroll once I run it as the screen would then go from 4 inch to 3.5.

Was it helpful?

Solution

I just ran this and it compiles and runs without error (although it doesn't do anything of note):

.h:

@interface ViewController2 : UIViewController {

    IBOutlet UIScrollView *scroller;

}    

.m:

@implementation ViewController2

- (void)viewDidLoad
{
    [super viewDidLoad];
    scroller = [[UIScrollView alloc] init];
    [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 600)];
}

OTHER TIPS

Ok, this is what you do.

In your .h file add the delegate.

@interface ViewController2 : UIViewController <UIScrollViewDelegate> {
     IBOutlet UIScrollView *scroller;
}

@property (nonatomic, retain) UIScrollView *scroller;
@end

In your implementation .m file be sure to add the synthesize statement for your scroller

#import "ViewController2.h"

@implementation ViewController2
@synthesize scroller;

Then in your viewDidLoad method override as follows:

 - (void)viewDidLoad {
        [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 600)];
    scroller.maximumZoomScale = 4.0;
    scroller.minimumZoomScale = 0.75;
    scroller.clipsToBounds = YES;
    //here is the delegate portion below
         scroller.delegate = self;
        [self.view bringSubviewToFront:scroller];
        [super viewDidLoad];
  }

Do not forget to connect it properly in Interface builder. Link scroller to your control!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top