UIImageView内容模式不适用于单元格选择

时间:2015-03-29 02:46:41

标签: ios objective-c uitableview cocoa-touch uiimageview

我在UITmageView上包含的UIImageView中有以下设置......

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

最初显示视图时,单元格中的图像大小适合UIImageView的尺寸。对于纵向图像,一切正常。但是,当UIImage以横向方向定位时,缩略图会水平扩展到图像的原始宽高比。

我尝试过多种不同的模式组合,但我无法保持横向图像的缩略图不会扩展。

关于如何保持缩略图正方形的任何想法,无论细胞的选择状态如何?我不需要保持纵横比,并且可以在必要时裁剪图像。

修改

我相信这是大通所暗示的......

#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

这对我所看到的行为没有影响,但确实会产生以下日志消息......

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 个答案:

答案 0 :(得分:0)

尝试添加一个设置高度和宽度彼此相等的约束

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

修改:使您的图片查看属性并覆盖:

-(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];
}

完成视图设置后,请致电[self setNeedsUpdateConstraints]。这将确保在正确的时间加载约束。