简体   繁体   English

UIImageView大小相对于屏幕大小

[英]UIImageView size relative to screen size

I have an UIImageView, with an image like so 我有一个UIImageView,像这样的图像

imageView.image = UIImage(named: "imageName")
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false

On the iPhone X this image looks great with 140x140 dimensions Maybe needless to say, this does not look great on the iPhone5s, since 140x140 would take up too much of screen space 在iPhone X上,此图像的尺寸为140x140时看起来不错,也许不用说,在iPhone5s上看起来不是很好,因为140x140会占用过多的屏幕空间

How can I correctly scale my image for other screen dimensions, what would be the best practice for this? 如何针对其他屏幕尺寸正确缩放图像,最佳做法是什么?

As for now, I set the image height and width to 至于现在,我将图像的高度和宽度设置为

let size = UIScreen.main.bounds.height / 5.5
imageView.heightAnchor.constraint(equalToConstant: size),
imageView.widthAnchor.constraint(equalToConstant: size),

Since that will be pretty close to what I am after. 因为那将非常接近我的追求。 But I can't shake the feeling that there are better alternatives than this 但我不能不感到有比这更好的选择的感觉

The question is a bit broad like "What do you think guys?", but still. 这个问题有点笼统,例如“您觉得伙计们如何?”,但仍然如此。


It's better to change your size accordingly to your parent view, not screen. 最好根据父视图而不是屏幕更改大小。

imageView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.2, constant: 0)
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor, multiplier: 1.0, constant: 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM