UIScrollView + PageControl

时间:2015-05-30 17:08:29

标签: ios objective-c uiscrollview uipagecontrol

我有一个带有scrollview(初始大小:390,170)和页面控制插件的视图控制器。我需要它来从我的数组中显示每页的图像。但我有意想不到的结果(见下图)。enter image description here

enter image description here

图片不是全角,第二页后滚动结束。

这是代码

- (void)viewDidLoad {
    [super viewDidLoad];
    //Put the names of our image files in our array.
    imageArray = [[NSArray alloc] initWithObjects:@"header1.jpg", @"header2.jpg", @"header3.jpg", nil];
    [self.articlesPageControll setNumberOfPages:imageArray.count];
    for (int i = 0; i < [imageArray count]; i++) {
        //We'll create an imageView object in every 'page' of our scrollView.
        CGRect frame;
        frame.origin.x = 390 * i;
        frame.origin.y = 0;
        frame.size = self.articlesScrollView.frame.size;

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        [imageView setContentMode:UIViewContentModeScaleToFill];
        imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
        [self.articlesScrollView addSubview:imageView];
    }
    //Set the content size of our scrollview according to the total width of our imageView objects.
    self.articlesScrollView.contentSize = CGSizeMake(self.articlesScrollView.frame.size.width * [imageArray count], 170);

}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    // Update the page when more than 50% of the previous/next page is visible
    CGFloat pageWidth = self.articlesScrollView.frame.size.width;
    int page = floor((self.articlesScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.articlesPageControll.currentPage = page;
}

UPD:第3页图像实际存在,但无法滚动到此页面

提前致谢!

1 个答案:

答案 0 :(得分:0)

解决方案self.articlesScrollView.contentSize = CGSizeMake(390 * [imageArray count], 170);