UIScrollView does not scroll in landscape mode - test code and screenshots included

In Xcode 5.0.2, I created a universal application and dragged a UIScrollViewand UIImageView(from its “View Mode” changed to “Top Left”) into the storyboards of the iPhone and iPad (here full screen mode ):

xcode screenshot

In an attempt to make it UIScrollViewfill the entire screen, I added 4 restrictions (here full screen mode ):

xcode screenshot

Then in ViewController.m I try:

  • Download board.png(copyright Wikipedia Commons, Denelson83)
  • Give UIImageViewthe same dimensions asboard.png
  • Set contentSizefor UIScrollViewthe same size.

Here is the source code:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    _scrollView.contentSize = _imageView.bounds.size;
}

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

    // fruitless attempt to solve the rotation problem
    _scrollView.contentSize = _imageView.bounds.size;
}

Here's the conclusion:

Scroll[3464:70b] -[ViewController viewDidLoad]: (1000.000000 x 961.500000)

, 2 :

1:

iPhone iPad- ( Cmd - ) - :

iphone 3.5 screenshot

ipad screenshot

2:

, :

iphone 4 screenshot

1:

iphonic (!), deviceOrientationDidChange, - :

Scroll[702:70b] -[ViewController viewDidLoad]: image size = {1000, 961.5}
Scroll[702:70b] -[ViewController deviceOrientationDidChange:]: view size = {1000, 961.5}
Scroll[702:70b] -[ViewController deviceOrientationDidChange:]: view size = {1000, 961.5}
Scroll[702:70b] -[ViewController deviceOrientationDidChange:]: view size = {1000, 961.5}

( _imageView.image.size, , _imageView.size 100 x 100 sotryboards):

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *img = [UIImage imageNamed:@"board"];
    NSLog(@"%s: image size = %@", __PRETTY_FUNCTION__, NSStringFromCGSize(img.size));
    _imageView.image = img;
    _imageView.frame = CGRectMake(0, 0, img.size.width, img.size.height);

    [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector: @selector(deviceOrientationDidChange:)
         name: UIDeviceOrientationDidChangeNotification
         object: nil];
}

-(void)deviceOrientationDidChange:(NSNotification *)notification

{
    _scrollView.contentSize = _imageView.image.size;
    NSLog(@"%s: view size = %@", __PRETTY_FUNCTION__, NSStringFromCGSize(_imageView.image.size));
}
+3
6
  • Orientation viewDidLayoutSubviews
  • UIScrollView contentSize viewDidLoad
  • AutoResizeSubview UIScrollView = XIB "" "" Clip Clip.

https://github.com/jayaprada-behera/ScrollViewOrientation

- (void)viewDidLoad
{
    [super viewDidLoad];
    //set uiscrollview.autoresizeSubview = NO; in xib
    UIImage *img = [UIImage imageNamed:@"board.png"];
    imageView_.image = img;

    imageView_.frame = CGRectMake(imageScrollView.frame.origin.x, imageScrollView.frame.origin.y, img.size.width, img.size.height);
    imageScrollView.contentSize = imageView_.bounds.size;
}

.

+1

.

  • bounds, , , bounds .

  • AutoLayout, .

  • , contentSize. .

    -(void)viewDidLoad
      {
        [super viewDidLoad];
    
        UIImage *img = [UIImage imageNamed:@"board"];
        NSLog(@"%s: (%f x %f)", __PRETTY_FUNCTION__, img.size.width, img.size.height);
        _imageView.image = img;
        _imageView.frame = CGRectMake(0, 0, img.size.width, img.size.height);
    
        //Handles rotation notifications
        [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil]; 
    }
    
    
    
    -(void)deviceOrientationDidChange:(NSNotification *)notification
    
    {
        _scrollView.contentSize = _imageView.frame.size;
    
        NSLog(@"Size = %@",NSStringFromCGSize(_imageView.frame.size));
    }
    
+1

.

  • .
  • .
  • .
  • .

1 , 2 3 , . , .

+1

. . , Autolayout Mainstoryboard. ,

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

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

    _scrollView.contentSize = _imageView.bounds.size;
}
+1

, , , . .

- ( ), Xcode . , , , .

, contentSize. contentSize .

Apple : https://developer.apple.com/library/ios/technotes/tn2154/_index.html

+1

:

In Interface Builder, I assigned the width and height UIImageViewand set its "View Mode" to "Bottom" (here fullscreen ):

Xcode screenshot

The following code in ViewController.m is sufficient (no rotation detection is needed):

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *img = [UIImage imageNamed:@"board"];
    _imageView.image = img;
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    _scrollView.contentSize = _imageView.bounds.size;
}
0
source

All Articles