简体   繁体   English

将UIImageview添加到另一个子视图UIScrollview后未显示

[英]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. 我只在代码中创建视图控制器,没有.xib或情节提要。 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 我在视图中添加了许多其他子视图,这些子视图是在方法(void)loadView中添加的,它们的框架是在(id)init方法中初始化的,如下所示

-(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: 我创建了一个空的scrollview,然后根据屏幕上的用户操作,我尝试使用以下方法每次向滚动视图中添加一个imageView:

-(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. 我的问题是将scrollview view添加到了主view但是没有显示imageview

I have debugged it and the scrollview has the subviews added and they have frame size and all, but not showing on the screen. 我已经调试了它,并且scrollview已添加了子视图,并且它们具有帧大小和全部大小,但未显示在屏幕上。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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