2013-10-12 141 views
0

我有UIScrollView和另一個UIImageView。我想要UIImageViewUIScrollView。我已經嘗試了所有bringSubviewToFrontinsertAtIndex的東西,但它不起作用。請幫助我!UIImageView的Z位置與UIScrollView的位置

這裏是代碼 -

對於UIScrollView

UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 300, 
                SCREEN_WIDTH, SCREEN_HEIGHT)]; 
scrollView.showsHorizontalScrollIndicator = FALSE; 
[self.view addSubview:scrollView]; 

__block int tagValue = 1; 
__block NSInteger tag = 1; 

for (int i=0; i<[listOfImages count]; i++) { 
    NSDictionary *myDic = [listOfImages objectAtIndex:i]; 
    NSString *urlImage = [myDic objectForKey:@"product_image"]; 
    //NSLog(@"%lu",(unsigned long)[listOfImages count]); 
    image = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, 0, 200, 140)]; 
    // [image setImage:[UIImage imageNamed:@"img_def.png"]]; 
    NSURL *imageURL = [NSURL URLWithString:urlImage]; 

    [image setImageWithURL:imageURL 
      placeholderImage:[UIImage imageNamed:@"img_def.png"]]; 

    image.tag = tag; 
    image.contentMode = UIViewContentModeScaleAspectFit; 

    [scrollView insertSubview:image atIndex:1]; 
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] 
          initWithTarget:self action:@selector(handleTap:)]; 
    recognizer.numberOfTapsRequired = 1; 
    recognizer.numberOfTouchesRequired = 1; 
    recognizer.delegate = self; 
    [image addGestureRecognizer:recognizer]; 
    [image setUserInteractionEnabled:YES]; 

    leftMargin += SCREEN_WIDTH; 
    tagValue += 1; 
    tag += 1; 

} 
[scrollView setContentSize:CGSizeMake(leftMargin, 0)]; 
[scrollView setPagingEnabled:YES]; 

而且圖像的代碼,我要來的滾動視圖的頂部 -

UIImage *originalPromo = [UIImage imageNamed:@"promo"]; 
    UIImage *scaledPromo = [UIImage imageWithCGImage:[originalPromo CGImage] 
    scale:(originalPromo.scale *2.0) orientation:(originalPromo.imageOrientation)]; 
    UIImageView *promo = [[UIImageView alloc] init]; 
    [promo setFrame:CGRectMake(45.5, 300.0, 
           scaledPromo.size.width, scaledPromo.size.height)]; 
    [promo setImage:scaledPromo]; 
    [self.view insertSubview:promo atIndex:100]; 

回答

0

而不是使用insertSubview:atIndex的使用addSubview:。添加子視圖時,它總是添加到已經在父視圖中的所有其他子視圖的頂部。