【发布时间】:2015-06-02 05:19:27
【问题描述】:
我在 UITableViewCell 中包含的 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.
【问题讨论】:
标签: ios objective-c uitableview cocoa-touch uiimageview