viewController.m
-(void) viewDidLoad
{
[super viewDidLoad];
[self addScoreRowViewWithIndexLeft:(++i) andName:[scoreDict objectForKey:@"name"] andScore:[[scoreDict objectForKey:@"points"] integerValue]];
}
-(void) addScoreRowViewWithIndexLeft:(NSInteger)index andName: (NSString *)name andScore: (NSInteger)score {
UIView *scoreRowView = [[UIView alloc] initWithFrame:CGRectMake(128, 112 + index * 80, 280, 20)];
UILabel *indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 20, 20)];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 20, 180, 20)];
UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(330, 20, 50, 20)];
UIColor *color = [UIColor darkGrayColor];
[indexLabel setText:[@((int)index) stringValue]];
[indexLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:18]];
[indexLabel setTextColor:color];
[nameLabel setText:name];
[nameLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:18]];
[nameLabel setTextColor:color];
[scoreLabel setText:[@((int)score) stringValue]];
[scoreLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:18]];
[scoreLabel setTextColor:color];
[scoreRowView addSubview:indexLabel];
[scoreRowView addSubview:nameLabel];
[scoreRowView addSubview:scoreLabel];
[self.view addSubview:scoreRowView];
}
我在我的故事板中添加了我的ImageView作为我的View的子视图。
当我删除ImageView时,一切正常。我显示了我的其他代码生成的子视图。但是当我在我的故事板中添加ImageView时,它在我的其他子视图之上。如果我给我的ImageView一个0.3的alpha值,我可以看到我的子视图通过。
我的代码出了什么问题?我也试过了;
[self.view bringSubviewToFront:scoreRowView];
但它没有用。
我该怎么办?
答案 0 :(得分:2)
尝试更改ImageView和scoreRowView的zPosition。你可以这样做:
在ViewDidLoad的底部添加类似的内容:
yourImageView.layer.zPosition = 1;
scoreRowView.layer.zPosition = 2;
我怀疑[self.view bringSubviewToFront:scoreRowView];
无法正常工作,因为您在ViewDidLoad中调用它,其中视图无法正常工作。您可以尝试在例如中调用它ViewDidAppear并检查它是否有效。