简体   繁体   中英

UIImageview not showing after being added to another subview which is UIScrollview

I am creating a view controller in code only, NO .xib or storyboard. I have many other subviews added to the view, which are added in a method (void)loadView and their frames are initialised in the (id)init method as shown below

-(id)init{ 

   bottomClinkRect = CGRectMake(playersBoardRect.origin.x + (playersBoardWidth * 0.320),
                                playersBoardRect.origin.y + playersBoardHeight - playerBottomImageRect.size.height -  (playersBoardHeight * 0.038),
                                playersBoardWidth * 0.680,
                                playersBoardHeight * 0.038);
}
- (void)loadView {

   bottomClink = [[UIScrollView alloc]initWithFrame:bottomClinkRect];
   [bottomClink setScrollEnabled:YES];
   bottomClink.contentSize = CGSizeMake(bottomClink.frame.size.height,bottomClink.frame.size.height);
   [contentView addSubview:bottomClink];
}

I create empty scrollview and then based on user actions on screen I am trying to add one imageView per time to the scrollview using the following method:

-(void)reloadCollections:(NSNotification *)notification
{
    UIImage *latestPieceCaptured = [gameController capturedBlackPiecesImages].lastObject;

    [whitePlayerCaptures addObject:latestPieceCaptured];

    NSInteger newNumberOfViews = whitePlayerCaptures.count;

    CGFloat xOrigin = (newNumberOfViews - 1) * bottomClink.frame.size.height;

    CGRect imageRect = CGRectMake(bottomClink.frame.origin.x + xOrigin,
                                  bottomClink.frame.origin.y,
                                  bottomClink.frame.size.height, //the width is taken from the height
                                  bottomClink.frame.size.height); // the height

    UIImageView *image = [[UIImageView alloc] initWithFrame:imageRect];
    image.backgroundColor = [UIColor blackColor];
    image.image = latestPieceCaptured;
    image.contentMode = UIViewContentModeScaleAspectFit;

    //set the scroll view content size
    bottomClink.contentSize = CGSizeMake(bottomClink.frame.size.height * newNumberOfViews,
                                         bottomClink.frame.size.height);

    [bottomClink  addSubview:image];
}

My problem is that the scrollview is added to the main view but the imageview (s) are not showing up.

I have debugged it and the scrollview has the subviews added and they have frame size and all, but not showing on the screen. I have tried so many variations but nothing has worked so far.

Thanks in advance for any links or advice.

That works for me:

- (void)addImagesToScrollView {

    [self.scrollViewOutlet setAlwaysBounceVertical:NO];

    _scrollViewOutlet.delegate = self;

    for (int i = 0; i < thumbnailPreview.count; i++) {
        CGFloat xOrigin = i * self.scrollViewOutlet.frame.size.width;
        UIImageView *image = [[UIImageView alloc] initWithFrame:
                              CGRectMake(xOrigin, 0,
                                         self.scrollViewOutlet.frame.size.width,
                                         self.scrollViewOutlet.frame.size.height)];
        image.image = [UIImage imageNamed: [thumbnailPreview objectAtIndex:i]];

            image.contentMode = UIViewContentModeScaleAspectFill;


       image.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;


        image.clipsToBounds = YES;

        [self.scrollViewOutlet addSubview:image];
    }
    //set the scroll view content size
    self.scrollViewOutlet.contentSize = CGSizeMake(self.scrollViewOutlet.frame.size.width *
                                                   thumbnailPreview.count,
                                                   self.scrollViewOutlet.frame.size.height);

}

And call it in

- (void)viewDidAppear:(BOOL)animated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM