UIScrollView不会通过自动调整大小调整大小

时间:2013-03-14 05:27:35

标签: ios xcode uiscrollview autosize

我在iOS 5+上支持UIScrollView的所有内容都表明我应该可以使用Xcode的Size Inspector中的自动调整功能来自动调整视图大小。

使用Storyboard,我有一个TabBarViewController,其中一个标签有一个UIScrollView和一个页面控件。

在幕后我已经以编程方式设置了UIView中页面的处理(我不知道是否有必要发布代码,但为了清楚起见我还是会这样做。)

从iPhone 3.5英寸切换到iPhone 4英寸时,自动调整大小无法正常工作。 我希望在使用3.5英寸屏幕时可以看到UIScrollView和Page控件。

我应该注意,iPad版本(请参阅下面的代码)在我的子视图中没有正确捕捉。 (这可能是一个完全不同的问题。)

4英寸屏幕 4 Inch Screen

3.5英寸屏幕 3.5 Inch Screen

以防万一,这是我的.m文件:

#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

3 个答案:

答案 0 :(得分:5)

enter image description here

像这样使用

enter image description here

像这样你的问题将解决

或在.m文件中编写单行代码

 scrollview.frame = CGRectMake(x, y, 320, self.view.bounds.size.height);

答案 1 :(得分:4)

只需检查添加了滚动视图的视图是否启用了AutoResizeSubviews。如果不是,它将不会根据屏幕尺寸调整滚动视图。

其次,根据3.5英寸屏幕调整滚动视图,AutoResizing将调整为4英寸屏幕。反之亦然。

对于页面控制,请这样做。它始终位于您视图的底部。

答案 2 :(得分:1)

在Supporting Files文件夹中选择yourappname.pch,并在文件底部的最后一个#endif之前添加此行 检查,设备....

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

然后在ViewWillAppear()

中写下这个
    if (IS_IPHONE5)
    {
        scrollView.contentSize = CGSizeMake(width,height );
    }