繁体   English   中英

将UIimageView带到前面

[英]Bring UIimageView to front

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

顺序为Controller >> 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]此行不执行任何操作

您应该在操作前将图像视图添加到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
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];

我得到像

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM