默认情况下,它看起来像aspect fit
将图像对齐到框架的底部。有没有办法在保持完整的同时覆盖aspect fit
对齐?
** 编辑 **
这个问题早于自动布局。事实上,汽车布局在 WWDC 2012 中被披露,就在这个问题被问到的同一周
默认情况下,它看起来像aspect fit
将图像对齐到框架的底部。有没有办法在保持完整的同时覆盖aspect fit
对齐?
** 编辑 **
这个问题早于自动布局。事实上,汽车布局在 WWDC 2012 中被披露,就在这个问题被问到的同一周
简而言之,您不能使用UIImageView
.
一种解决方案是对UIView
包含 an 的a 进行子类化,UIImageView
并根据图像大小更改其框架。例如,您可以在此处找到一个版本。
将UIImageView
的底部布局约束优先级设置为最低(即 250),它将为您处理。
这样做的方法是修改 UIImageView 层的 contentsRect。我的项目(UIImageView 的子类)中的以下代码假定 scaleToFill 并偏移图像,使其对齐顶部、底部、左侧或右侧,而不是默认的中心对齐。对于 aspectFit 将是一个类似的解决方案。
typedef NS_OPTIONS(NSUInteger, AHTImageAlignmentMode) {
AHTImageAlignmentModeCenter = 0,
AHTImageAlignmentModeLeft = 1 << 0,
AHTImageAlignmentModeRight = 1 << 1,
AHTImageAlignmentModeTop = 1 << 2,
AHTImageAlignmentModeBottom = 1 << 3,
AHTImageAlignmentModeDefault = AHTImageAlignmentModeCenter,
};
- (void)updateImageViewContentsRect {
CGRect imageViewContentsRect = CGRectMake(0, 0, 1, 1);
if (self.image.size.height > 0 && self.bounds.size.height > 0) {
CGRect imageViewBounds = self.bounds;
CGSize imageSize = self.image.size;
CGFloat imageViewFactor = imageViewBounds.size.width / imageViewBounds.size.height;
CGFloat imageFactor = imageSize.width / imageSize.height;
if (imageFactor > imageViewFactor) {
//Image is wider than the view, so height will match
CGFloat scaledImageWidth = imageViewBounds.size.height * imageFactor;
CGFloat xOffset = 0.0;
if (BM_CONTAINS_BIT(self.alignmentMode, AHTImageAlignmentModeLeft)) {
xOffset = -(scaledImageWidth - imageViewBounds.size.width) / 2;
} else if (BM_CONTAINS_BIT(self.alignmentMode, AHTImageAlignmentModeRight)) {
xOffset = (scaledImageWidth - imageViewBounds.size.width) / 2;
}
imageViewContentsRect.origin.x = (xOffset / scaledImageWidth);
} else if (imageFactor < imageViewFactor) {
CGFloat scaledImageHeight = imageViewBounds.size.width / imageFactor;
CGFloat yOffset = 0.0;
if (BM_CONTAINS_BIT(self.alignmentMode, AHTImageAlignmentModeTop)) {
yOffset = -(scaledImageHeight - imageViewBounds.size.height) / 2;
} else if (BM_CONTAINS_BIT(self.alignmentMode, AHTImageAlignmentModeBottom)) {
yOffset = (scaledImageHeight - imageViewBounds.size.height) / 2;
}
imageViewContentsRect.origin.y = (yOffset / scaledImageHeight);
}
}
self.layer.contentsRect = imageViewContentsRect;
}
class AlignmentImageView: UIImageView {
enum HorizontalAlignment {
case left, center, right
}
enum VerticalAlignment {
case top, center, bottom
}
// MARK: Properties
var horizontalAlignment: HorizontalAlignment = .center
var verticalAlignment: VerticalAlignment = .center
// MARK: Overrides
override var image: UIImage? {
didSet {
updateContentsRect()
}
}
override func layoutSubviews() {
super.layoutSubviews()
updateContentsRect()
}
// MARK: Content layout
private func updateContentsRect() {
var contentsRect = CGRect(origin: .zero, size: CGSize(width: 1, height: 1))
guard let imageSize = image?.size else {
layer.contentsRect = contentsRect
return
}
let viewBounds = bounds
let imageViewFactor = viewBounds.size.width / viewBounds.size.height
let imageFactor = imageSize.width / imageSize.height
if imageFactor > imageViewFactor {
// Image is wider than the view, so height will match
let scaledImageWidth = viewBounds.size.height * imageFactor
var xOffset: CGFloat = 0.0
if case .left = horizontalAlignment {
xOffset = -(scaledImageWidth - viewBounds.size.width) / 2
}
else if case .right = horizontalAlignment {
xOffset = (scaledImageWidth - viewBounds.size.width) / 2
}
contentsRect.origin.x = xOffset / scaledImageWidth
}
else {
let scaledImageHeight = viewBounds.size.width / imageFactor
var yOffset: CGFloat = 0.0
if case .top = verticalAlignment {
yOffset = -(scaledImageHeight - viewBounds.size.height) / 2
}
else if case .bottom = verticalAlignment {
yOffset = (scaledImageHeight - viewBounds.size.height) / 2
}
contentsRect.origin.y = yOffset / scaledImageHeight
}
layer.contentsRect = contentsRect
}
}
这将使图像填充宽度并仅占用适合图像所需的高度(宽泛地说)
斯威夫特 4.2:
let image = UIImage(named: "my_image")!
let ratio = image.size.width / image.size.height
cardImageView.widthAnchor
.constraint(equalTo: cardImageView.heightAnchor, multiplier: ratio).isActive = true
我有类似的问题。最简单的方法是创建自己的 UIImageView 子类。我添加了子类 3 属性,所以现在可以在不知道内部实现的情况下轻松使用它:
@property (nonatomic) LDImageVerticalAlignment imageVerticalAlignment;
@property (nonatomic) LDImageHorizontalAlignment imageHorizontalAlignment;
@property (nonatomic) LDImageContentMode imageContentMode;
我通过在 UIImageView 的高度上设置约束来在 Interface Builder 中本地解决了这个问题,因为当图像大于屏幕尺寸时,图像总是会被“向上推”。
更具体地说,我将 UIImageView 设置为与其所在的 View 相同的高度(通过高度约束),然后在 IB 中使用间距约束定位 UIImageView。这导致 UIImageView 具有“Aspect Fit”,它仍然尊重我在 IB 中设置的顶部间距约束。
如果您能够子类UIImageView
化,那么您可以覆盖image
var。
override var image: UIImage? {
didSet {
self.sizeToFit()
}
}
在 Objective-C 中,你可以通过重写 setter 来做同样的事情。