2012-03-23 43 views
0

我已经制作了我自己的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); 

}

的问题是,认为出现的时候,它是黑色的。该按钮仅在滚动视图时出现。

我错过了什么?

回答

0

听起来像是你不触发重绘行动 - 从documentation

当您的视图更改的实际内容,这是你的责任通知你的观点需要重新绘制系统。您可以通过调用视图的setNeedsDisplay或setNeedsDisplayInRect方法来实现此目的。这些方法让系统知道它应该在下一个绘图周期中更新视图。因为它等到下一个绘图周期才更新视图,所以可以在多个视图中调用这些方法以同时更新它们。

+0

我试过把setneedsdisplay放在drawrect的结尾或在createView中,但是不会改变任何东西。 – 2012-03-23 17:07:56