IPhone : Why is this UIImage and UILabel collapsed?

on Tuesday, February 24, 2015

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:


enter image description here


When I switch to a namedImage, I got this:


enter image description here


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