Slider257
Slider257

Reputation: 69

Getting UIScrollView to work correctly on iPad

2ND UPDATE

Courtesy of this thread
In iOS6, XCode 4.5, UIScrollView is not Scrolling, despite contentSize being set

the answer has been provided! Turning off Auto Layout on your Storyboard (the entire thing) will fix ScrollView and allow things to scroll properly. Spread the word people!

UPDATE Added code. Seriously cannot figure out why this will not work on a iPad.

.h file

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

.m file

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
self.scrollView.contentSize = CGSize(768, 1600);
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Again just to clarify I'm using Storyboards, I'm first dragging in a ScrollView, setting the size to standard iPad screen (768x1004 because I have a Nav bar at the top) then adding my image on top of that and setting it's size (768x1600). Then I connect both of these to the header file (as shown above) and still I can't get the image to scroll. I have my view mode set to "Aspect Fit", could this be the issue? I've tried other settings and no luck but not all of them.

Any specific advice would be so appreciated!

ORIGINAL POST

I know there a number of topics out there regarding this one, but I'm having a dog of a time trying to get a ScrollView to work. I am attempting to load an image that is 768x1600 (on an iPad screen only) so that you have to vertically scroll to see more of the image's content. To create the ScrollView, I use a Storyboard and drag a ScrollView into the ViewController frame, align it and ensure interactions are enabled. Then, on top of the ScrollView (also in Storyboard) I add the UIImage I wish to use (ensuring it is a subview) and size it accordingly, then set interactions enabled as well.

No matter how hard I try, I cannot get this to work in Simulator or on my device. I have made sure that my ScrollView is set as a delegate in the IB, that is has an IBOutlet on the header file and is both synthesized and loaded into my implementation file. I have also ensured that I have set contentSize by;

self.scrollView.contentSize = CGSizeMake(768, 1600);

I have also tried to add the UIImage as a Subview via code (as opposed to on the Storyboard) and when I do this the image does not appear when I run Simulator. On top of this, when I click the page no scrolling option is available.

Any help anyone can give me on this one is much appreciated. If code is necessary I will post it.

Upvotes: 1

Views: 2347

Answers (1)

Undo
Undo

Reputation: 25697

I think I just figured it out. You say you drug the UIImageView on top of the scroll view.

UIImageView's aren't user-interactable!

Try selecting your image view in Interface Builder and setting 'User interaction enabled' to YES for the image view.

If that doesn't work, I would suggest adding the image view in code (remove it from IB first):

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage.png"]];
    self.imageView.frame = CGRectMake(0,0,768,1600);
    self.imageView.userInteractionEnable = YES;
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = CGSizeMake(768, 1600);
    self.scrollView.frame = self.view.bounds;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Upvotes: 2

Related Questions