简体   繁体   English

如何使用约束在interfaceBuilder中调整UIImageView的大小(用于3.5”和4”)

[英]How to resize a UIImageView (for 3.5“ and 4”) in interfaceBuilder with constrain

I am trying to put an UIImageView into the UIView of my ViewController, an resize UIImageView automatically when the UIView is either 480px or 568px height. 我试图将UIImageView放入ViewController的UIView中,当UIView的高度为480px或568px时,会自动调整UIImageView的大小。 But it seems incredibly complex in IB! 但这在IB中似乎非常复杂!

If i put a normal view inside my View controller, it will resize naturally. 如果我在View控制器中放一个普通视图,它将自然调整大小。 But with my UIImageView it doesn't. 但是用我的UIImageView却没有。 Even if I choose the scaleToFill Mode. 即使我选择了scaleToFill模式。 Nothing will happen. 什么都不会发生。

So it seems I need to use constrains. 因此,似乎我需要使用约束。 But the problem is when I add a simple constraint just to tell the UIImageView to be X height size. 但是问题是当我添加一个简单的约束只是为了告诉UIImageView为X高度大小时。 IB tell me there are errors, and ask me to add lots of constraints to other component of the view. IB告诉我有错误,并要求我向视图的其他组件添加很多约束。 That's propagating and I am finishing with thousands of constraints. 那正在传播,我正在完成成千上万的约束。 This is confusing, since I just want to make my UIView resize to the Parent container and for IB to tell me I need to add a constraint to other components who never ask nothing. 这很令人困惑,因为我只想将UIView的大小调整为Parent容器,并且为了让IB告诉我,我需要向其他从未提出要求的组件添加约束。

In code it take only two lines! 在代码中只需两行! I don't understand why it's so complex in IB. 我不明白为什么它在IB中是如此复杂。 So could someone please explain me how to make my UIImageView resize according to his parent ? 所以有人可以向我解释如何根据他的父母调整UIImageView的大小吗?

I have made lot of research in Google, and found nothing that helped me. 我在Google上进行了大量研究,但发现没有任何帮助。 It is like nobody never has to use full imageView mode. 就像没有人永远不必使用完整的imageView模式一样。

Note that I have already succeed with constraint for a full UIImageView in another viewController, but this is kind of obscure. 请注意,我已经在另一个viewController中成功使用完整UIImageView的约束,但这有点晦涩。 But now I don't know why when I do the same things, IB want to add lot of lot of lot of constraints. 但是现在我不知道为什么当我做同样的事情时,IB会添加很多很多约束。

I think it would be better if you'd use constraints with distance to the superview (ie the view controller), instead of height/width constraints. 我认为,如果将约束与超级视图的距离(即视图控制器)一起使用,而不是高度/宽度约束,那会更好。 Select your image view and add constraints like this: 选择图像视图并添加如下约束:

在此处输入图片说明

The image view will automatically fill the screen on both 3,5" and 4" iPhones. 图像视图将在3,5“和4” iPhone上自动填充屏幕。 Afterwards just play around with the image view's contentMode to get the desired effect. 之后,只需使用图像视图的contentMode即可获得所需的效果。

Using autolayout or the the old autoresizing masks is the way to solve the problem. 使用自动布局或旧的自动调整大小蒙版是解决问题的方法。
Marko has pointed the solution, but is missing something depending on your requirements. Marko指出了解决方案,但根据您的要求缺少一些东西。 With the contentMode you can play around to adapt the image to fit, or fill the bounds of the UIImageView with or without keeping the aspect ratio. 使用contentMode您可以contentMode调整图像以使其适合,或在保持或不保持纵横比的情况下填充UIImageView的边界。
One issue that I had in the past was to load 2 different images one for 3.5 inches and the other for 4 inches screen. 我过去遇到的一个问题是加载2个不同的图像,一个加载3.5英寸,另一个加载4英寸的屏幕。 Apple say that you can use this naming convention to load the correct image at runtime. 苹果公司说,您可以使用此命名约定在运行时加载正确的图像。

  • name~iphone.png 名〜iphone.png
  • name@2x~iphone.png name@2x~iphone.png
  • name~ipad.png 名〜ipad.png
  • name@2x~ipad.png name@2x~ipad.png
  • name-568h@2x~iphone.png (iPhone 5) 名称-568h@2x~iphone.png(iPhone 5)

It works well for retina and different devices (iphone or ipad), but the iphone5 convention works only for launch images. 它适用于视网膜和其他设备(iphone或ipad),但iphone5约定仅适用于启动图像。
Now there is only one opportunity left, check the screen size and load the correct one at runtime, in my opinion this is not an awesome solution. 现在只剩下一个机会了,检查屏幕尺寸并在运行时加载正确的机会,我认为这不是一个很棒的解决方案。
In Xcode 5 thank to the Images asset functionality is possible to load the right image for a specific screen size. 在Xcode 5中,感谢图像资产功能,可以为特定的屏幕尺寸加载正确的图像。 Just add a new image set, convert in the attribute inspector to device specific and just drag the images on their slot. 只需添加一个新图像集,在属性检查器中将其转换为特定于设备的图像,然后将图像拖到其插槽中即可。
在此处输入图片说明

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

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