简体   繁体   English

如何使用自动布局调整固定宽度和高度的UIImageView的大小

[英]How to resize a fixed width and height UIImageView with auto layout

I have a view named myView which is always half the screen and sit at the top of screen. 我有一个名为myView的视图,该视图始终位于屏幕的一半,并且位于屏幕顶部。

Inside this view I added an UIImageView with 120x120 size which sits in center of the myView (horizontally and vertically). 在此视图内,我添加了一个120x120尺寸的UIImageView ,它位于myView中心(水平和垂直)。 Inside the IB, to satisfy the constraints(X & Y) I always need to set a fixed width and height for my image, after I set center horizontally and center vertically. 在IB内部,为了满足约束条件(X&Y),在水平居中和垂直居中后,我总是需要为图像设置固定的宽度和高度。

But with a fixed height and width, the image doesn't resize when changing the screen size. 但是使用固定的高度和宽度,更改屏幕尺寸时图像不会调整大小。 I want my image to resize when running on iPhone5 or iPhone 4s, because myView will resize. 我希望在iPhone5或iPhone 4s上运行时调整图像大小,因为myView会调整大小。 I need something like the image should depend on the myView size. 我需要类似图像的东西,具体取决于myView大小。

How to actually achieve this ? 如何实际实现呢?

set imageView.clipToBounds = yes , and also set imageView.contentMode = UIViewContentModeScaleAspectFit; 设置imageView.clipToBounds = yes ,还设置imageView.contentMode = UIViewContentModeScaleAspectFit;

Update You are talking about imageView not the image, in that case it will not resize due to constant width and height, do one thing, create IBoutLet of constraint and change there values when required, or you can also set Aspect ratio with superView 更新您是在谈论imageView而不是图像,在这种情况下,由于恒定的宽度和高度,图像将不会调整大小,做一件事,创建IBoutLet约束并在需要时更改该值,或者您也可以使用superView设置纵横比

If you set the image with fixed width and height it will not change respectively. 如果将图像设置为固定的宽度和高度,则不会分别更改。 You can either set it to be relative to myView or you can instead set leading and trailing size from all for edges and delete the center constrains. 您可以将其设置为相对于myView ,也可以设置所有边缘的前导和尾随尺寸并删除中心约束。

Your constraints should look like this: 您的约束应如下所示:

在此处输入图片说明

For Swift 4: Updating answer of @Adnan Aftab 对于Swift 4:更新@Adnan Aftab的答案

imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFit

暂无
暂无

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

相关问题 UIImageView-固定宽度和灵活高度? - UIImageView - Fixed width & Flexible height? 如何在不使用固定高度和宽度的情况下使用自动布局维护圆形uiview - How maintain a circular uiview using auto layout without fixed height and fixed width 如何在iOS中自动调整UIButton的高度和宽度? - How to Auto resize UIButton height and width in IOS? 如何以编程方式将UIImageView缩放为固定宽度和灵活高度? - How to programmatically scale a UIImageView to a fixed width and flexible height? 具有固定宽度的UIImageView:如何设置其高度以保持其宽高? - UIImageView with fixed width: how to set its height to keep its aspect? 如何调整UITextView的大小,当达到一定值时,它会根据内容和固定宽度自动调整其宽度和高度? - How to resize a UITextView that automatically resize its width and height based on the content and fixed width when reaches certain value? 如何检测UIImageView的宽度和高度 - How to detect the width and height of a UIImageView 如何通过使用自动布局显示和隐藏组件来调整UITableViewCell高度的大小 - How to resize UITableViewCell height by showing and hiding components using auto layout 如何在横向旋转中使用自动布局调整UIPickerView / UIDatePickerView高度? - How to resize UIPickerView / UIDatePickerView height with auto layout in landscape rotation? 如何使用固定宽度和自动布局正确缩放图像? - How do i scale an image correctly with fixed width and auto layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM