我希望我的UIImageView增长或缩小,取决于它所显示的实际图像的大小。但我希望它保持垂直中心和10分,从前沿的超级视图。
但是,如果设置这两个约束,它会抱怨我没有设置足够的约束来满足自动布局。在我看来,它似乎完美地描述了我想要的东西。我遗漏了什么?
发布于 2018-01-27 13:47:38
对于@algal案例3,我所要做的就是
class ScaledHeightImageView: UIImageView {
    override var intrinsicContentSize: CGSize {
        if let myImage = self.image {
            let myImageWidth = myImage.size.width
            let myImageHeight = myImage.size.height
            let myViewWidth = self.frame.size.width
            let ratio = myViewWidth/myImageWidth
            let scaledHeight = myImageHeight * ratio
            return CGSize(width: myViewWidth, height: scaledHeight)
        }
        return CGSize(width: -1.0, height: -1.0)
    }
}这将缩放intrinsicContentSize的高度组件。(-1.0,-1.0)在没有图像时返回,因为这是超类中的默认行为。
此外,我不需要为包含图像视图的单元格的表设置UITableViewAutomaticDimension,而且单元格仍然自动大小。图像视图的内容模式是Aspect Fit。
https://stackoverflow.com/questions/26833627
复制相似问题