【问题标题】:UIImageview not showing after being added to another subview which is UIScrollviewUIImageview 被添加到另一个子视图 UIScrollview 后不显示
【发布时间】:2016-07-10 21:21:10
【问题描述】:

我只在代码中创建视图控制器,没有 .xib 或情节提要。 我在视图中添加了许多其他子视图,它们是在 (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];
}

我创建了空的滚动视图,然后根据屏幕上的用户操作,我尝试使用以下方法每次将一个 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];
}

我的问题是 scrollview 被添加到主 viewimageview(s) 没有出现。

我已经对其进行了调试,scrollview 添加了子视图,它们具有帧大小等所有内容,但未显示在屏幕上。我尝试了很多变化,但到目前为止没有任何效果。

提前感谢您提供任何链接或建议。

【问题讨论】:

    标签: ios objective-c uiscrollview uiimageview addsubview


    【解决方案1】:

    这对我有用:

    - (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);
    
    }
    

    然后调用它

    - (void)viewDidAppear:(BOOL)animated
    

    【讨论】:

    • 它对我不起作用!我已经尝试了您的代码,但实际的滚动视图中没有出现任何内容,因为图像视图已添加到滚动视图但不可见,它甚至没有给我任何错误
    • 我也在尝试按需添加图像,而不是在视图加载或任何起始位置时添加图像,这就是为什么我添加了观察者,以便在满足某些条件时调用此方法。
    • 检查是否在 Assets.xcassets 或 bundle 中添加图片。
    • 我有图像,当我在上述函数中设置断点并将鼠标悬停在图像上然后单击下拉菜单中的眼睛图标时,我看到了实际图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 2014-03-03
    相关资源
    最近更新 更多