I'm trying to put a UIImage + UILabel in a UICollectionViewCell.
If I'm only to use a background color in that UIImage, it works perfectlly:
When I switch to a namedImage, I got this:
As you can see, the label become invisible somehow, I couldn't tell why.
Attached code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setImage: [UIImage imageNamed:@"avatar"]];
// [imageView setBackgroundColor: [UIColor yellowColor]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.translatesAutoresizingMaskIntoConstraints = NO;
UILabel *label = [[UILabel alloc] init];
[label setTextAlignment: NSTextAlignmentCenter];
[label setText:@"aaaa"];
[label setTextColor: [UIColor whiteColor]];
[label setBackgroundColor: [UIColor redColor]];
label.translatesAutoresizingMaskIntoConstraints = NO;
[cell addSubview: label];
[cell addSubview: imageView];
[cell sendSubviewToBack: imageView];
[cell addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-0-[imageView]-0-|"
options:NSLayoutFormatAlignAllBaseline metrics:0
views:NSDictionaryOfVariableBindings(imageView, label)]];
[cell addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-0-[label]-0-|"
options:NSLayoutFormatAlignAllBaseline metrics:0
views:NSDictionaryOfVariableBindings(imageView, label)]];
[cell addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|-0-[imageView]-30-[label]-0-|"
options:NSLayoutFormatAlignAllLeft metrics:0
views:NSDictionaryOfVariableBindings(imageView, label)]];
[cell setBackgroundColor: [UIColor greenColor]];
return cell;
}
0 comments:
Post a Comment