将UIimageView带到前面

时间:2015-10-01 07:36:30

标签: ios objective-c iphone uiimageview

我有一个ViewController,其中有一个UIScrollView。在那我有一个UIView和UIView我必须添加标签和图像。图像应该在我的标签上方。

订单为控制器>> ScrollView>> UIView>> UILAbel>> UIImageView

但我无法将UIimageView添加到视图中。

这是我的代码

 scrollView =[[UIScrollView alloc]init];
[scrollView setFrame:CGRectMake(0, 0, 320, 480)];
 [scrollView setContentSize:CGSizeMake(320,2820)];
[scrollView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
[scrollView setScrollEnabled:YES];
[scrollView setShowsHorizontalScrollIndicator:YES];
[scrollView setShowsVerticalScrollIndicator:NO];
[self.view addSubview:scrollView];


    UIView *firstView = [[UIView alloc]initWithFrame:CGRectMake(5, 5, 310, 300)];
firstView.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:firstView];


UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(65, 3, 180, 50)];
label.text = @"ABC";
label.textAlignment =NSTextAlignmentCenter;
label.textColor = [UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
label.font=[UIFont fontWithName:@"Helvetica-Bold" size:18];
[firstView addSubview:label];

 imgView = [[UIImageView alloc]initWithFrame:CGRectMake(115, 0, 90, 90)];
imgView.image = [UIImage imageNamed:@"thumbs4.png"];
[self.view bringSubviewToFront:imgView];

[self.view bringSubviewToFront:imgView] 此行无效

2 个答案:

答案 0 :(得分:1)

您应该在操作之前将图像视图添加到self.view。但如果它是最后添加的子视图,则没有必要将它带到前面。

imgView = [[UIImageView alloc]initWithFrame:CGRectMake(115, 0, 90, 90)];
imgView.image = [UIImage imageNamed:@"thumbs4.png"];
[self.view addSubview:imgView] // added as subview
[self.view bringSubviewToFront:imgView]; // not really needed. imgView is actually in front

答案 1 :(得分:1)

UIScrollView *scrollView =[[UIScrollView alloc]init];
[scrollView setFrame:CGRectMake(0, 0, 320, 480)];
[scrollView setContentSize:CGSizeMake(320,2820)];
[scrollView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
[scrollView setScrollEnabled:YES];
[scrollView setShowsHorizontalScrollIndicator:YES];
[scrollView setShowsVerticalScrollIndicator:NO];
[self.view addSubview:scrollView];

UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(115, 0, 90, 90)];
imgView.image = [UIImage imageNamed:@"dream.jpg"];
[self.view addSubview:imgView];

UIView *firstView = [[UIView alloc]initWithFrame:CGRectMake(5, 5, 310, 300)];
firstView.backgroundColor = [UIColor blueColor];
[scrollView addSubview:firstView];


UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(65, 3, 180, 50)];
label.text = @"ABC";
label.textAlignment =NSTextAlignmentCenter;
label.textColor = [UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
label.font=[UIFont fontWithName:@"Helvetica-Bold" size:18];
[firstView addSubview:label];

我输出像

enter image description here