将 UIimageView 放在前面
Posted
技术标签:
【中文标题】将 UIimageView 放在前面【英文标题】:Bring UIimageView to front 【发布时间】:2015-10-01 07:36:29 【问题描述】:我有一个 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] 这行什么都不做
【问题讨论】:
试试:[scrollView addSubview:imgView];
@TusharJ。 - 是的,它在添加到滚动视图后出现,但我希望它的框架是固定的。因为我不希望它被滚动
在我看来,理想的方法是将imgView
添加到self.view
并使scrollView
透明。但我认为,您正在为scrollView
[scrollView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
提供背景颜色。试试这个:[self.view addSubview:imgView]; [self.view bringSubviewToFront:imgView];
要将超级视图的子视图放在前面,首先必须将其添加到超级视图中。
@TusharJ。 - 谢谢好友
【参考方案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];
我得到了类似的输出
【讨论】:
是的,谢谢@Anbu.Karthik【参考方案2】:您应该在操作之前将图像视图添加到 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
【讨论】:
以上是关于将 UIimageView 放在前面的主要内容,如果未能解决你的问题,请参考以下文章
iphone - 单个 UIImage 多个 UIImageView?
将 UIImage 用于 UIImageView 时 UIImage 失真