2013-03-13 24 views
1

我將子視圖添加到UIScrollView。對於這個問題,我簡化了添加的視圖:testView它包含一個UIButtonUIScrollView子視圖並非總是可點擊

我的滾動視圖是偉大的工作,但上的按鈕觸摸都不盡如人意

    只在(近似)100的第一像素
  • 我可以點擊第一個按鈕,但
  • 滾動工作得很好。
  • 我不能在第一個按鈕結束單擊
  • 我不能點擊其他按鈕

這裏是我的代碼:

__block CGFloat scrollViewContentSize = 0; 
__block CGFloat buttonRectOrigineY = 0; 

[self.itemsToDisplay enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 

    CGRect frameTest = CGRectMake(0, buttonRectOrigineY/2, 320, 200); 
    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    testButton.frame = frameTest; 
    UIView *testView = [[UIView alloc] initWithFrame:frameTest]; 
    [testView addSubview:testButton]; 
    [self.articleScrollView addSubview:testView]; 
    buttonRectOrigineY  += 200; 
    scrollViewContentSize += 200; 
}]; 
[self.articleScrollView setContentSize:CGSizeMake(320, scrollViewContentSize)]; 

這裏是圖像很好地理解我的問題: uiscrollview with button not touchable

回答

0

我弄錯了我的按鈕和視圖的框架。這裏有一個可以幫助任何人的工作代碼。

__block CGFloat scrollViewContentSize = 0; 
__block CGFloat buttonRectOrigineY = 0; 

[self.itemsToDisplay enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 

    CGRect frameTestButton = CGRectMake(0, 0, 300, 150); 
    CGRect frameTestView = CGRectMake(10, buttonRectOrigineY, 300, 200); 
    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    testButton.frame = frameTestButton; 
    testButton.backgroundColor = [UIColor greenColor]; 
    UIView *testView = [[UIView alloc] initWithFrame:frameTestView]; 
    if(idx == 0) 
     testView.backgroundColor = [UIColor yellowColor]; 
    if(idx == 1) 
     testView.backgroundColor = [UIColor orangeColor]; 
    if(idx == 2) 
     testView.backgroundColor = [UIColor redColor]; 
    if(idx == 3) 
     testView.backgroundColor = [UIColor magentaColor]; 
    if(idx == 4) 
     testView.backgroundColor = [UIColor purpleColor]; 
    if(idx <= 4){ 
     LogDebug(@"idx : %lu", (unsigned long)idx); 
     [testView addSubview:testButton]; 
     [self.articleScrollView addSubview:testView]; 
     [self.articleScrollView bringSubviewToFront:testView]; 
     buttonRectOrigineY  += 200; 
     scrollViewContentSize += 200; 
    } 
}]; 
[self.articleScrollView setContentSize:CGSizeMake(320, scrollViewContentSize)]; 
0

您可以使用bringSubviewToFront來避免該問題

CGRect frameTest = CGRectMake(0, buttonRectOrigineY/2, 320, 200); 
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
testButton.frame = frameTest; 
UIView *testView = [[UIView alloc] initWithFrame:frameTest]; 
[testView addSubview:testButton]; 
[testView bringSubviewToFront:testButton]; 
[self.articleScrollView addSubview:testView]; 
[self.articleScrollView bringSubviewToFront:testView]; 

如果您再次遇到問題,請在按鈕周圍應用邊框並檢查手動顯示的按鈕區域。

+0

謝謝,我把一些'backgroundcolor'來理解我的意見和按鈕,以及在我的按鈕上的一些視圖。不管怎樣,謝謝你 – 2013-03-14 17:53:04