cha*_*yWu 20 uiview uiimageview uiimage ios
我想用时控制图像排列UIViewContentModeScaleAspectFit在UIImageView.

例如,我有两个UIImageView一个视图如上所述.这两个UIImageView的contentMode是UIViewContentModeScaleAspectFit.现在我想将图像3设置为右对齐,将图像4设置为左对齐,以便这两个图像可以在一起.
我尝试将contentMode设置为UIViewContentModeScaleAspect|UIViewContentModeLeft,但它会让事情变得更糟.
任何建议表示赞赏.
cha*_*yWu 32
最后,我找到了可以解决它的UIImageViewAligned项目.
感谢@Marchy,还有快速版UIImageViewAlignedSwift
您可以以编程方式向图像视图添加宽度约束,以便不存在"多余空间",而不是尝试在图像视图中左对齐图像.
假设您具有UIImageView"Aspect Fit"内容模式,并在界面构建器中添加了固定高度约束.然后,在相关视图控制器中,检查图像的纵横比并应用必要的宽度约束将图像视图捕捉到包含的图像.例如:
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// UIImage loaded from somewhere...
imageView.image = uiImage
// Check the UIImageView for existing height constraints
let heightConstraints = imageView.constraints.filter{$0.firstAttribute == .height}
if let imageViewHeight = heightConstraints.first?.constant {
// Calculate the width which would make the image view fit
// the image perfectly, and constrain the view to that width.
let desiredImageViewWidth = (imageViewHeight / uiImage.size.height) * uiImage.size.width
imageView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredImageViewWidth))
}
}
Run Code Online (Sandbox Code Playgroud)
要将此问题应用于您的问题,您可以将两者约束UIImageView为彼此相邻,为两者(具有相同值)设置固定高度约束,然后使用上面的代码以编程方式设置宽度.
对于某些人来说,另一个选择可能是调整上的内容拥抱优先级UIImageView。UIImageView在 Interface Builder 中添加一个具有低优先级的约束。
这将满足约束条件,直到添加图像。然后将 UIImageView 的内容拥抱优先级设置为较高的宽度值(如果要顶部/底部对齐,则设置高度)。
然后只需将 UIImageView 的内容模式设置为您想要的即可。希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
15860 次 |
| 最近记录: |