【问题标题】:UIImageView content mode not working on cell selectUIImageView 内容模式不适用于单元格选择
【发布时间】: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


    【解决方案1】:

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

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

    编辑:将您的 imageView 设为属性并覆盖:

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

    【讨论】:

    • 试一试,但没有乐趣。这必须是一个常见的用例,因为我看到大量应用程序中的缩略图不会调整大小。我不知道为什么我不去探索魔法。
    • @DavidNedrow 您是否使用 xib 来布局您的自定义单元格?
    • 不,只是继承 UITableViewCell
    • @DavidNedrow 还有 David 看看 i.stack.imgur.com/JHiqw.png 以确保 Aspect 填充是您想要的。
    猜你喜欢
    • 2018-03-06
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多