Iphone UIScrollView在子视图上方有空白

Iphone UIScrollView在子视图上方有空白,iphone,ios,objective-c,scrollview,Iphone,Ios,Objective C,Scrollview,我遇到了一个奇怪的问题,我似乎无法理解。 在向ScrollView添加多个子视图时,ScrollView会继续在常规图像的顶部添加空白 颜色越多越好: 它应该是这样的: 图像完全可以滚动 这是我用来创建scrollview的代码: -(void)addImagesToScrollView { if (self.product != nil) { int imageCount = 0; if (self.product.labelImage != nil)

我遇到了一个奇怪的问题,我似乎无法理解。
在向ScrollView添加多个子视图时,ScrollView会继续在常规图像的顶部添加空白

颜色越多越好:

它应该是这样的:

图像完全可以滚动

这是我用来创建scrollview的代码:

-(void)addImagesToScrollView
{
  if (self.product != nil)
  {
    int imageCount = 0;

    if (self.product.labelImage != nil)
    {
      UIImageView *labelImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
      labelImage.image = [UIImage imageWithData:self.product.labelImage];
      labelImage.contentMode = UIViewContentModeScaleAspectFit;
      labelImage.tag = 100;
      [self.scrollView addSubview:labelImage];
      imageCount += 1;
    }

    if (self.product.bottleImage != nil)
    {
      UIImageView *bottleImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
      bottleImage.image = [UIImage imageWithData:self.product.bottleImage];
      bottleImage.contentMode = UIViewContentModeScaleAspectFit;
      bottleImage.tag = 101;
      imageCount += 1;

      [self.scrollView addSubview:bottleImage];
    }
    if (self.product.pourImage != nil)
    {
      UIImageView *pourImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * 2, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
      pourImage.image = [UIImage imageWithData:self.product.pourImage];
      pourImage.contentMode = UIViewContentModeScaleAspectFit;
      pourImage.tag = 102;
      imageCount += 1;
      [self.scrollView addSubview:pourImage];
    }
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageCount, self.scrollView.frame.size.height);
    [self.scrollView setContentOffset:CGPointZero animated:NO];

  }
}
编辑:

背景色:

使用:


它将让您看到UIImageView的帧,可能您计算的帧不正确,请使用下面的代码,您将获得所需的输出。在下面的代码中,您的应用程序将使用相册照片,并根据相册中的图像数自动设置滚动的大小。请根据您的图像库进行修改

-(void)getAllPictures
{


    imageArray=[[NSArray alloc] init];
    mutableArray =[[NSMutableArray alloc]init];
    NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

    library = [[ALAssetsLibrary alloc] init];

    void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != nil) {
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) {
                             [mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];

                             if ([mutableArray count]==count)
                             {
                                 imageArray=[[NSArray alloc] initWithArray:mutableArray];
                                 [self allPhotosCollected:imageArray];
                             }
                         }
                        failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ]; 

            } 
        }
    };

    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            count=[group numberOfAssets];
        }
    };

    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                         failureBlock:^(NSError *error) {NSLog(@"There is an error");}];
}



-(void)allPhotosCollected:(NSArray*)imgArray
{


    CGSize imageSize;

    imageSize.width= imageView.bounds.size.width;
    imageSize.height = imageView.bounds.size.height;
    self.scrollView=[[UIScrollView alloc]init];
    self.scrollView.frame = imageView.frame;
    self.scrollView.contentSize = CGSizeMake(imageSize.width * imgArray.count, imageSize.height);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces=YES;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
    [self.scrollView flashScrollIndicators];
    [self.view addSubview:self.scrollView];

    CGFloat xPos = 0.0;

    for (UIImage *image in imgArray) {
            imageView = [[UIImageView alloc] initWithImage:image];
            imageView.frame = CGRectMake(xPos, 0.0, imageSize.width, imageSize.height);


         [self.scrollView addSubview:imageView];

        xPos += imageSize.width;
        // assuming ARC, otherwise release imageView

    }



}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20,320 , 420)];
    [self getAllPictures];
}

添加-图像似乎没有完全拉伸?请尝试UIViewContentModeScaleToFill
-(void)getAllPictures
{


    imageArray=[[NSArray alloc] init];
    mutableArray =[[NSMutableArray alloc]init];
    NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

    library = [[ALAssetsLibrary alloc] init];

    void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != nil) {
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) {
                             [mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];

                             if ([mutableArray count]==count)
                             {
                                 imageArray=[[NSArray alloc] initWithArray:mutableArray];
                                 [self allPhotosCollected:imageArray];
                             }
                         }
                        failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ]; 

            } 
        }
    };

    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            count=[group numberOfAssets];
        }
    };

    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                         failureBlock:^(NSError *error) {NSLog(@"There is an error");}];
}



-(void)allPhotosCollected:(NSArray*)imgArray
{


    CGSize imageSize;

    imageSize.width= imageView.bounds.size.width;
    imageSize.height = imageView.bounds.size.height;
    self.scrollView=[[UIScrollView alloc]init];
    self.scrollView.frame = imageView.frame;
    self.scrollView.contentSize = CGSizeMake(imageSize.width * imgArray.count, imageSize.height);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces=YES;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
    [self.scrollView flashScrollIndicators];
    [self.view addSubview:self.scrollView];

    CGFloat xPos = 0.0;

    for (UIImage *image in imgArray) {
            imageView = [[UIImageView alloc] initWithImage:image];
            imageView.frame = CGRectMake(xPos, 0.0, imageSize.width, imageSize.height);


         [self.scrollView addSubview:imageView];

        xPos += imageSize.width;
        // assuming ARC, otherwise release imageView

    }



}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20,320 , 420)];
    [self getAllPictures];
}