2012-07-26 87 views
0

我根據數組元素的在用於loop.The容器視圖的數目增加子視圖到我的滾動視圖具有附加的子視圖。裏面的問題是,只有所述第一容器的觀點包含添加子視圖和新的沒有,但第三個也有它。我檢查了子視圖的框架,但似乎仍然沒有工作。添加子視圖滾動型,其具有額外的子視圖

CGFloat contentOffset = 0.0f; 
//add tej views one by one to scrollview 

for (NSString *packageType in packageTypesArray) 
{ 

    CGRect lvPackageContainerFrame = CGRectMake(contentOffset, 0.0f,scrollView.frame.size.width,scrollView.frame.size.height); 

    LVPackageContainer *lvPackageContainer = [[LVPackageContainer alloc] initWithFrame:lvPackageContainerFrame]; 

    NSLog(@"Frame %@",NSStringFromCGRect(lvPackageContainerFrame)); 
    lvPackageContainer.packageType = packageType; 
    [packageContainersArray addObject:lvPackageContainer]; 

    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 264, 314)]; 
    NSLog(@"BackgroundImage %@",backgroundImage); 
    backgroundImage.backgroundColor = [UIColor redColor]; 
    [backgroundImage setImage:[UIImage imageNamed:@"shadowBackground.png"]]; 
    backgroundImage.center = lvPackageContainer.center; 
    [lvPackageContainer addSubview:backgroundImage]; 


    [scrollView addSubview:lvPackageContainer]; 

    contentOffset += lvPackageContainerFrame.size.width; 
    scrollView.contentSize = CGSizeMake(contentOffset, scrollView.frame.size.height); 

} 
+0

如果我刪除行backgroundImage.center = lvPackageContainer.center ;視圖被添加,但我需要設置確切的座標來找到中心。中心有什麼問題? – 2012-07-26 10:26:34

回答

1
backgroundImage.center = CCGPointMake(lvPackageContainerFrame.size.width/2, lvPackageContainerFrame.size.height/2); 

如果添加一個子視圖,該座標系是從它的父視圖

開始在你的情況,當你設置中心第二種觀點,它會成爲像{480240}(假設視圖在iphone全屏幕) 因此,第三子視圖一個你可以看到實際上是頁面的子視圖2.

更新: 當你得到一個視圖的中心,它實際上從幀計算值:

center.x = (frame.origin.x + frame.size.width)/2; 
center.y = (frame.origin.y + frame.size.height)/2; 

所以假設你的第二個觀點是CGRectMake(320, 0, 320, 480),它的中心將是

center.x = (320 + 320)/2; 
center.y = (0 + 480)/2; 

所以你的子視圖將在第二視圖{480,240}中心。

如果你將其轉換相對於滾動視圖,子視圖將在{(320+480),(0+240)},你會看到它是3號視圖,而不是第二個一個

+0

是什麼,但我不明白的是,如果在容器視圖的座標確定,當我嘗試將視圖添加到cgPoint其中之一(0,0)是相對父容器view.why容器的確切起源view.center失敗仍mysery – 2012-07-26 19:10:13

+0

我我的答案更新 – Hanon 2012-07-27 02:34:27

相關問題