0
votes

I have the following set on my UIImageView contained in a UITableViewCell...

imageView.opaque = YES;
imageView.layer.cornerRadius = 5;
imageView.layer.masksToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFill;

When the view is initially displayed, the image in the cell is properly sized to the dimensions of the UIImageView. For portrait oriented images everything works fine. However, when a cell whose UIImage is landscape oriented, the thumbnail expands horizontally to the native aspect ratio of the image.

I've tried a number of different mode combinations, but I can't keep the thumbnail for landscape images from expanding.

Any ideas on how I can keep the thumbnail square, regardless of the selected state of the cell? I don't need to maintain the aspect ratio, and the image can be cropped if necessary.

EDIT

This, I believe, is what Chase was suggesting...

#import "ChildCell.h"

@interface ChildCell ()
@property (nonatomic,strong,readonly) UIImageView *imageView;
@end

@implementation ChildCell

- (void)layoutSubviews {
    [super layoutSubviews];

    self.imageView.opaque = YES;
    self.imageView.layer.cornerRadius = 5;
    self.imageView.layer.masksToBounds = YES;
//    self.imageView.contentMode = UIViewContentModeScaleToFill;
    [self setNeedsUpdateConstraints];
}

-(void)updateConstraints{
    NSLayoutConstraint *constraint = [NSLayoutConstraint
                                      constraintWithItem:self.imageView
                                      attribute:NSLayoutAttributeWidth
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.imageView
                                      attribute:NSLayoutAttributeHeight
                                      multiplier:1.0
                                      constant:0];
    constraint.priority = 1000;
    [self.imageView addConstraint:constraint];
    [super updateConstraints];
}

@end

This doesn't make a difference in the behavior I'm seeing, but does yield the following log message...

2015-03-29 17:01:54.054 RedditingRK[32813:871715] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
"<NSAutoresizingMaskLayoutConstraint:0x7fb742f161d0 h=--& v=--& H:[UIImageView:0x7fb742e19480(70)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7fb742f16860 h=--& v=--& V:[UIImageView:0x7fb742e19480(39)]>",
"<NSLayoutConstraint:0x7fb742d8c9a0 UIImageView:0x7fb742e19480.width == UIImageView:0x7fb742e19480.height>"
)

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7fb742d8c9a0 UIImageView:0x7fb742e19480.width == UIImageView:0x7fb742e19480.height>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.

The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
1

1 Answers

0
votes

Try adding a constraint that sets the height and width equal to each other

NSLayoutConstraint *constraint = [NSLayoutConstraint
    constraintWithItem:imageView
    attribute:NSLayoutAttributeWidth
    relatedBy:NSLayoutRelationEqual
    toItem:imageView
    attribute:NSLayoutAttributeHeight
    multiplier:1.0
    constant:0];
constraint.priority = 1000;
[imageView addConstraint:constraint];

Edit: Make your imageView a property and override:

-(void)updateConstraints{
 NSLayoutConstraint *constraint = [NSLayoutConstraint
    constraintWithItem:self.imageView
    attribute:NSLayoutAttributeWidth
    relatedBy:NSLayoutRelationEqual
    toItem:self.imageView
    attribute:NSLayoutAttributeHeight
    multiplier:1.0
    constant:0];
constraint.priority = 1000;
 [self.imageView addConstraint:constraint];
 [super updateConstraints];
}

After you're done setting up your views call [self setNeedsUpdateConstraints]. This will make sure the constraint is loaded at the right time.