0

我有一个UIScrollView和另一个UIImageView。我要UIImageView过来UIScrollView。我已经尝试了所有的bringSubviewToFront东西insertAtIndex,但它不起作用。请帮帮我!

这是代码 -

对于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];
4

1 回答 1

0

而不是使用insertSubview:atIndexuse addSubview:。添加子视图时,它总是添加在父视图中已经存在的所有其他子视图之上。

于 2013-10-12T18:53:44.943 回答