简体   繁体   English

固定宽度,可变高度的图像视图ios布局

[英]Fixed width, variable height image view ios layout

I want an imageview which has fixed width but variable height. 我想要一个固定宽度但高度可变的imageview。 I want to refresh view if the image that I get from the website is heighter than the imageview in storyboard, 如果我从网站上获得的图像比情节提要中的imageview高,我想刷新视图,

This is the view when I use "Scale to Fill". 这是我使用“缩放以填充”时的视图。

postimg.org/image/6l1ol2pvz postimg.org/image/6l1ol2pvz

And this is when I use "Aspect Fill" 这是当我使用“方面填充”时

postimg.org/image/9duw53q8f/ postimg.org/image/9duw53q8f/

I want the image to be like in "Aspect Fill" but I can't place the bottom correctly. 我希望图像像“纵横填满”中那样,但是我无法正确放置底部。

Thank you all! 谢谢你们!

Solution: 解:

I gave my image width and height constraints. 我给出了图像的宽度和高度限制。 Then I changed it programmatically. 然后我以编程方式对其进行了更改。 This is the view I wanted and I made it work! 这是我想要的视图,并且使它起作用了!

http://i.stack.imgur.com/PCgHw.png http://i.stack.imgur.com/PCgHw.png

let imageData = NSData(contentsOfURL: imgUrl)
let myImage = UIImage(data: imageData!)
let aspectRatio = (myImage?.size.height)! / (myImage?.size.width)!

self.postCombineImageViewHeight.constant = UIScreen.mainScreen().bounds.size.width * aspectRatio
self.postCombineImageViewWidth.constant = UIScreen.mainScreen().bounds.size.width

Thank you all for your help! 谢谢大家的帮助!

You should select "clip subviews" in the "Attributes inspector" of this image view in IB (or programmatically set the clipToBounds property of the image view). 您应该在IB中此图像视图的“属性检查器”中选择“剪辑子视图”(或以编程方式设置图像视图的clipToBounds属性)。

For example, this is a landscape image shown in a square image view with a content model of "aspect fill", but with "clip subviews" unchecked (ie with clipToBounds set to false ): 例如,这是在方形图像视图中显示的风景图像,其内容模型为“长宽比”,但未选中“剪辑子视图”(即, clipToBounds设置为false ):

在此处输入图片说明

(I've added a red CAShapeLayer to show where the frame of the UIImageView is.) (我添加了一个红色的CAShapeLayer来显示UIImageViewframe在哪里。)

This is the same image view, but with "clip subviews" checked (ie clipToBounds property is true ): 这是相同的图像视图,但是选中了“剪辑子视图”(即clipToBounds属性为true ):

在此处输入图片说明


Alternatively, you could choose "aspect fit", rather than "aspect fill", in which case the whole image will be visible and not distorted, but you may end up with blank space on the edges of the photo: 另外,您可以选择“宽高比”而不是“宽高比”,在这种情况下,整个图像将可见并且不会变形,但最终可能会在照片边缘留出空白:

在此处输入图片说明

您可以在情节提要中设置高度约束,然后在viewcontroller中拖放约束IBOutlet并在从网站获取imageview时更改其常量

Are you using storyboards? 您正在使用情节提要吗? And auto layout? 和自动布局?

Try this: place a "vertical distance" constraint between the bottom of the photo and the top of the view that is on the bottom. 尝试以下操作:在照片的底部和底部的视图顶部之间放置一个“垂直距离”约束。

Then click the added constraint and on the Attributes Inspector change the relation to "Greater Than or Equal". 然后单击添加的约束,然后在“属性”检查器上将关系更改为“大于或等于”。

This way, the imageView height will adapt to the image content; 这样,imageView的高度将适应图像内容。 but if the image's height grows too large, it will stop growing when it reaches the bottom view. 但是如果图像的高度太大,到达底视图时它将停止增长。

My solution - 我的解决方案-

1) Provide a height and width constraint for your image. 1)为图像提供高度和宽度约束。

2) Import AVFoundation kit. 2)导入AVFoundation套件。

3) Generate a new rect for your image by the following code(fitting it via aspect ratio) - 3)通过以下代码(通过宽高比拟合)为您的图像生成一个新的rect-

  let boundingRect =  CGRect(x: 0, y: 0, width: width, height: CGFloat(MAXFLOAT))//width is your fixed width for your image eg. - if your image covers width of your superview. It can be - self.view.frame.width

          let rect  = AVMakeRectWithAspectRatioInsideRect(photo.image.size, boundingRect)

So now you got your rect - 所以现在你有了自己的直肠-

Now you can change your height constraint by having something like this - 现在,您可以通过以下方式更改身高限制-

imageHeightConstraint.constant = rect.size.height

You can do it programmatically using NSLayoutConstraint 您可以使用NSLayoutConstraint编程方式进行NSLayoutConstraint

self.addConstraint(NSLayoutConstraint(
        item: myButton,
        attribute: .Width,
        relatedBy: .Equal,
        toItem: nil,
        attribute: .NotAnAttribute,
        multiplier: 1.0,
        constant: 200))

And for the height you add the variable height 对于高度,您可以添加可变高度

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

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