UIScrollView with UIButton:按钮仅在滚动后出现

时间:2012-03-23 16:50:37

标签: objective-c uiscrollview uibutton

我已经制作了自己的UIScrollView

- (void)drawRect:(CGRect)rect中,我按下了这样的按钮:

- (void) createView {
CGFloat currentY = 0;
CGFloat currentX = 0;
CGFloat currentH = 0;

for (int i = 0; i < [images count]; i++) {
    UIImage *img = [images objectAtIndex:i]; 

    CGSize cropParam = [[finalCrops objectAtIndex:i] CGSizeValue];
    CGSize imgSize = [[finalSizes objectAtIndex:i] CGSizeValue];

    UIImage *newImg = nil;

    if (imgSize.width > 0) {
        newImg = [img resize:imgSize];
    }

    if (cropParam.width > 0) {
        newImg = [newImg crop:CGRectMake(0, 0, cropParam.width, cropParam.height)];
    }

    // Create a button for the image
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(currentX + margin, 
                                                                  currentY + margin, 
                                                                  newImg.size.width, 
                                                                  newImg.size.height)];

    [button setImage:newImg forState:UIControlStateNormal];
    [self addSubview:button];

    currentX += 2 * margin + newImg.size.width;

    if (currentX > viewWidth) {
        currentX = 0;
        currentY += 2 * margin + newImg.size.height; 
        currentH = newImg.size.height;
    }
}
self.contentSize = CGSizeMake(viewWidth, currentY);

}

问题是当视图出现时,它是黑色的。仅在滚动视图时才会显示该按钮。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

听起来你没有触发重绘动作 - 来自documentation

  

当视图的实际内容发生变化时,您有责任通知系统您的视图需要重新绘制。您可以通过调用视图的视图的setNeedsDisplay或setNeedsDisplayInRect:方法来完成此操作。这些方法让系统知道它应该在下一个绘图周期中更新视图。因为它会等到下一个绘图周期来更新视图,所以您可以在多个视图上调用这些方法来同时更新它们。