Alexander Farber
Alexander Farber

Reputation: 22978

Added UIScrollView and UIImageView in storyboard - the image is squished and does not scroll

In Xcode 5.0.2 I have created a universal app and dragged a UIScrollView and an UIImageView into the iPhone and iPad storyboards (here fullscreen):

Xcode screenshot

Then I am trying to load the images board.png (copyright Wikipedia Commons, Denelson83) by the following code in ViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *img = [UIImage imageNamed:@"board"];
    NSLog(@"%s: img=%@ (%f x %f)", __PRETTY_FUNCTION__,
          img, img.size.width, img.size.height);
    _imageView.image = img;
    _scrollView.contentSize = img.size;
}

However the result is squished and the image can't be scrolled:

-[ViewController viewDidLoad]: img=<UIImage: 0x10912d770> (1000.000000 x 961.500000)

app screenshot

Any advice please? I suspect the image is cut off at the bottom, because I run the 3.5-inch iPhone simulator? How to fix this too?

UPDATE:

I am trying to add four 0-constraints to the scroll view, so that it takes the whole screen, but when I rotate the simulator with Cmd-Arrow - the scroll view doesn't resize (here fullscreen of Xcode):

constraints screenshot

rotated app

UPDATE 2:

When I change the UIImageView "view mode" to "Top Left" in Xcode storyboard and the source code to the following:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *img = [UIImage imageNamed:@"board"];
    _imageView.image = img;
    _imageView.frame = CGRectMake(0, 0, img.size.width, img.size.height);
    _scrollView.contentSize = img.size;
}

then I get the following result, which can not be scrolled:

app screenshot

and can not be rotated either (does not fill the screen):

rotated app

Upvotes: 1

Views: 3488

Answers (1)

eobet
eobet

Reputation: 303

Remember that the Storyboard image view already has its size set from the storyboard, and you never resize anything after you have loaded your image in your code. So, this is off the top of my head, but try:

UIImage *image = [UIImage imageNamed:@"board"];
_imageView.image = image;
_imageView.bounds = CGRectMake(0, 0, image.size.width, image.size.height);


- (void)viewDidLayoutSubviews
{
    _scrollView.contentSize = _imageView.bounds.size;
}

Upvotes: 5

Related Questions