[英]constrain for uiimageview to keep aspect ratio and expand based on device size
i have this problem in 2 projects.Normally, profile image is displayed in circle like 我在2个项目中有这个问题。通常情况下,个人资料图像以圆形显示
and this is ok for iphone5 size.problem raises on bigger devices.like 这对于iphone5 size.problem可以在更大的设备上提升
as you can see size of profile imageview, it looks small compare to cover image.if i let it increase based on outer image then xocde ignores the aspect ratio constrains which results in non cricular profile imageview 正如你可以看到轮廓图像视图的大小,它看起来比封面图像小。如果我让它基于外部图像增加然后xocde忽略宽高比约束,这导致非cricular轮廓图像
here's current constrains image 这是当前约束图像
any suggestion how should i handle it? 任何建议我应该如何处理它?
Update here's result if i set constrain on profile imageview to increase with ratio to background image. 如果我将轮廓图像视图上的约束设置为与背景图像的比率增加,则更新此处的结果。
asks for y of image view,as i want it to be center so i sets fixed hight = 68 in hopes that xcode will break this constrain to keep aspect ratio, result 请求y的图像视图,因为我希望它是中心,所以我设置固定的高度= 68希望xcode将打破这个约束,以保持纵横比,结果
so if i remove hight = 68 and add constrain like width. 所以如果我删除hight = 68并添加像宽度一样的约束。
result 结果
Select your image view and it's super view together and give equal width constraint
, 选择你的图像视图和它的超级视图,并给出equal width constraint
,
then select that equal width constraint
from size inspector
, and reduce its multiplier
until you get your desired width! 然后从size inspector
选择equal width constraint
,并减小其multiplier
直到获得所需的宽度!
Now select your image view and give aspect ratio
constraint! 现在选择图像视图并给出aspect ratio
约束!
And finally give position constraint for x and y position! 最后给出x和y位置的位置约束!
In your case center x
and center y
with it's super view I think! 在你的案例center x
和center y
,我认为它是超级视图!
And your imageview
will increase or decrease with device width and will remains square always! 并且您的imageview
将随着设备宽度的增加或减少而变化,并且始终保持正方形!
And You should set dynamic corner radius
(half of width or height of your imageview) in layoutSubviews
method of tableviewcell's class as you need rounded imageview and your size is dynamic! 你应该在tableviewcell类的layoutSubviews
方法中设置动态corner radius
(imageview的宽度或高度的一半),因为你需要圆形的imageview,你的大小是动态的!
Instead of setting the height and width as constants, set equal width constraints between the background image and the profile image: 不要将高度和宽度设置为常量,而是在背景图像和轮廓图像之间设置相等的宽度约束:
And then select the profile image and set the multiplier value to the desired value: 然后选择配置文件图像并将乘数值设置为所需的值:
Now you can also set an aspect ratio for the profile image. 现在,您还可以为配置文件图像设置纵横比。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.