首页 文章

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

提问于
浏览
0

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

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

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

我已经尝试了许多不同的模式组合,但我无法保持横向图像的缩略图不会扩展 .

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

EDIT

我相信这是大通建议的......

#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

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

    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: 使您的图像查看属性并覆盖:

    -(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] . 这将确保在正确的时间加载约束 .

相关问题