3

Everything I've read on supporting UIScrollView on iOS 5+ states that I should be able to use the autosizing feature within Xcode's Size Inspector to auto resize my views.

Using Storyboards, I have a TabBarViewController of which one of my tabs has a UIScrollView and a Page Control.

Behind the scenes I have setup programmatically a handling of the pages in a UIView (I don't know if it's necessary to post the code, but I'm going to do it anyway for clarity).

When switching from the iPhone 3.5inch to the iPhone 4inch the auto resize is not working whatsoever. I'd like to have the UIScrollView AND the Page control visible when using the 3.5 inch screen.

I should note that the iPad version (see code below) doesn't snap properly in my subview. (That may be a different question, altogether).

4 Inch Screen 4 Inch Screen

3.5 Inch Screen 3.5 Inch Screen

Just in case, here's my .m file:

#import "TutorialViewController.h"

@interface TutorialViewController ()
@property (nonatomic, strong) NSArray *pageImages;
@property (nonatomic, strong) NSMutableArray *pageViews;

- (void)loadVisiblePages;
- (void)loadPage:(NSInteger)page;
- (void)purgePage:(NSInteger)page;

#ifdef UI_USER_INTERFACE_IDIOM
#define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#else
#define IS_IPAD() (false)
#endif


@end

@implementation TutorialViewController

@synthesize scrollView = _scrollView;
@synthesize pageControl = _pageControl;

@synthesize pageImages = _pageImages;
@synthesize pageViews = _pageViews;



- (void)loadPage:(NSInteger)page {
    if (page < 0 || page >= self.pageImages.count) {
        // If it's outside the range of what you have to display, then do nothing
        return;
    }

    // 1
    UIView *pageView = [self.pageViews objectAtIndex:page];
    if ((NSNull*)pageView == [NSNull null]) {
        // 2
        CGRect frame = self.scrollView.bounds;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0.0f;

        // 3
        UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
        newPageView.contentMode = UIViewContentModeScaleAspectFill;
        newPageView.frame = frame;
        [self.scrollView addSubview:newPageView];
        // 4
        [self.pageViews replaceObjectAtIndex:page withObject:newPageView];
    }
}

- (void)purgePage:(NSInteger)page {
    if (page < 0 || page >= self.pageImages.count) {
        // If it's outside the range of what you have to display, then do nothing
        return;
    }

    // Remove a page from the scroll view and reset the container array
    UIView *pageView = [self.pageViews objectAtIndex:page];
    if ((NSNull*)pageView != [NSNull null]) {
        [pageView removeFromSuperview];
        [self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
    }
}

- (void)loadVisiblePages {
    // First, determine which page is currently visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));

    // Update the page control
    self.pageControl.currentPage = page;

    // Work out which pages you want to load
    NSInteger firstPage = page - 1;
    NSInteger lastPage = page + 1;


    // Purge anything before the first page
    for (NSInteger i=0; i<firstPage; i++) {
        [self purgePage:i];
    }

    // Load pages in our range
    for (NSInteger i=firstPage; i<=lastPage; i++) {
        [self loadPage:i];
    }

    // Purge anything after the last page
    for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
        [self purgePage:i];
    }

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // Load the pages that are now on screen
    [self loadVisiblePages];
    // NSLog(@"Scroll View Did Scroll");
}


- (void)viewDidLoad {
    [super viewDidLoad];

    // Step 1
    if (IS_IPAD())
    {
        self.pageImages = [NSArray arrayWithObjects:
                           [UIImage imageNamed:@"1536x2048 tutorial_1.png"],
                           [UIImage imageNamed:@"1536x2048 tutorial_2.png"],
                           [UIImage imageNamed:@"1536x2048 tutorial_3.png"],
                           [UIImage imageNamed:@"1536x2048 tutorial_4.png"],
                           nil];
    }
    else
    {
        self.pageImages = [NSArray arrayWithObjects:
                           [UIImage imageNamed:@"640x960 tutorial_1.png"],
                           [UIImage imageNamed:@"640x960 tutorial_2.png"],
                           [UIImage imageNamed:@"640x960 tutorial_3.png"],
                           [UIImage imageNamed:@"640x960 tutorial_4.png"],
                           nil];
    }


    NSInteger pageCount = self.pageImages.count;

    // Step 2
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = pageCount;

    // Step 3
    self.pageViews = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < pageCount; ++i) {
        [self.pageViews addObject:[NSNull null]];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // Step 4
    // 3.5in height = 388
    // 4in height = 476
    CGSize pagesScrollViewSize = self.scrollView.frame.size;
    self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);

    // Step 5
    [self loadVisiblePages];
}

@end
Macness
  • 1,226
  • 2
  • 13
  • 24

3 Answers3

5

enter image description here

use like this

or

enter image description here

like this your problem will solve

or write single line code in your .m file

 scrollview.frame = CGRectMake(x, y, 320, self.view.bounds.size.height);
Pratik
  • 2,399
  • 17
  • 36
  • I did this for both the Page control AND the UIScrollView and this did not work. Am I missing something? – Macness Mar 14 '13 at 05:35
  • do this for scroll view and for page control same as you have – Pratik Mar 14 '13 at 05:37
  • Same result. I setup the scroll view like both of your examples and I have autosizing ONLY for bottom on the page control (I just want it to stay on top of the tab bar. No Luck. :( – Macness Mar 14 '13 at 05:39
  • if you have write code for resizing in your .m file then just comment it and try again – Pratik Mar 14 '13 at 05:53
  • I don't think any of my code is effecting the resize. Plus, wouldn't I still be able to see a preview of the resize when I toggle the button that applies the 3.5 or 4 inch screen in storyboard view? Is there a way to set up the scrollview.size.height programmatically? – Macness Mar 14 '13 at 05:59
  • 1
    you can set height as scrollview.frame = CGRectMake(x, y, 320, self.view.bounds.size.height); try yhis – Pratik Mar 14 '13 at 06:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26133/discussion-between-macness-and-pratik) – Macness Mar 14 '13 at 06:12
  • I'm accepting this answer because @pratik ultimately gave me an alternative that worked. – Macness Mar 27 '13 at 19:51
4

Just check if the view in which you have added the scroll view has enabled AutoResizeSubviews. If not it will not adjust the scrollview according to the screen size.

Secondly, adjust your scroll view according to 3.5 inch screen and AutoResizing will adjust it for 4 inch screen. Don't do it vice-versa.

For page control, Do it this way. It will always be at the bottom of your view.

Swati
  • 1,417
  • 1
  • 15
  • 35
  • This didn't work for me. I had to setup both the scroll view and the page control programmatically using CGRectMake and finding the top level view's height to shift them. – Macness Mar 14 '13 at 07:14
  • 1
    "Just check if the view in which you have added the scroll view has enabled AutoResizeSubviews". This was my problem. – Xavi Gil May 13 '13 at 08:58
1

select yourappname.pch in the Supporting Files folder and add this line right before the last #endif at the bottom of the file to Check, for device....

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)

then write this in your ViewWillAppear()

    if (IS_IPHONE5)
    {
        scrollView.contentSize = CGSizeMake(width,height );
    }
Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
Ravi
  • 11
  • 3