ios Xcode 6:如何使图像视图填满所有设备的屏幕?自动布局不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27197799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 6: How to make an image view fill the screen on all devices? Auto layout not working?
提问by skyguy
So I have an image view and a button. I need the image view to be the correct size (not look squished or stretched) on all devices and more importantly I need it to be the size of the screen. I need the button to stay in about the same place on all devices.
所以我有一个图像视图和一个按钮。我需要图像视图在所有设备上都是正确的大小(不要看起来被压扁或拉伸),更重要的是我需要它是屏幕的大小。我需要按钮在所有设备上保持在大致相同的位置。
Back with Xcode 5 I remember auto layout fixing this problem but now that does not work. I looked at https://www.youtube.com/watch?v=G53PuA_TlXkand tried control-dragging to the view controller and centering the view both horizontally and vertically, but I can't get it to scale to the size of the screen. The video doesn't address this.
回到 Xcode 5 我记得自动布局修复了这个问题,但现在不起作用了。我查看了https://www.youtube.com/watch?v=G53PuA_TlXk并尝试控制拖动到视图控制器并将视图水平和垂直居中,但我无法将其缩放到屏幕。视频没有解决这个问题。
Is there a specific option I need to choose when control-dragging to keep the image view scaled? What should I do?
在控制拖动以保持图像视图缩放时是否需要选择特定选项?我该怎么办?
回答by vacawama
You need to do 2 things:
你需要做两件事:
- Set up your constraints properly
- Choose the type of scaling you want.
- 正确设置约束
- 选择所需的缩放类型。
Setting Up Your Constraints:
设置约束:
- Drag out an Image View and place it roughly in the center of your view.
- Set your size class to wAnyhAny.
- Click on the Pin icon |-[]-|at the bottom of the screen.
- Turn on all four orange I-beams. Set the four constants to zero.
- Uncheck the Constrain to marginscheckbox.
- Click Add 4 constraints
- 拖出一个图像视图并将其大致放置在视图的中心。
- 将您的尺码等级设置为 w Anyh Any。
- 单击|-[]-|屏幕底部的固定图标。
- 打开所有四个橙色工字梁。将四个常量设置为零。
- 取消选中约束到边距复选框。
- 点击 Add 4 constraints
Choosing Your Scaling:
选择您的缩放比例:
- Click on the Image View you added to your View Controller.
- In the Attributes Inspectoron the right, choose your image.
- Set the Modeof the Viewto Aspect Fill(fill entire screen cropping top/bottom or left/right as necessary) or Aspect Fit(letter box image so that entire image is shown uncropped).
- 单击您添加到视图控制器的图像视图。
- 在右侧的属性检查器中,选择您的图像。
- 设置模式中的视图来看点填充(填充整个屏幕裁剪上/下或左/右视需要)或看点飞度(信箱图像,显示未裁剪是整个图像)。
You can view the constraints that have been created for you. In the document outline, you should see 4 constraints under your image view:
您可以查看为您创建的约束。在文档大纲中,您应该在图像视图下看到 4 个约束:
Click on a constraint and then view them in the Attributes Inspectoron the right. Here are the 4 that I created:
单击一个约束,然后在右侧的Attributes Inspector 中查看它们。这是我创建的 4 个:
回答by maledr53
This is what worked for me:
这对我有用:
View Mode:
Aspect Fill
视图模式:
纵横填充
As constraints for the image:
Align Center X to: Superview
Equal width to: Superview
作为图像的约束:
Align Center X to: Superview
Equal width to: Superview
The 'Equal width'property is the one that scale the image to the size of the screen.
该“相等的宽度”属性是一个将图像缩放到屏幕的尺寸。
If you want to have the button in a specific position, set constraints from it to the view, the main ones you want to consider are a top constraint and any left or right constraints. So you are telling the view where you want to position the button in relation to its sides.
如果您想将按钮放在特定位置,请设置从它到视图的约束,您要考虑的主要约束是顶部约束和任何左或右约束。因此,您是在告诉视图要将按钮相对于其侧面放置的位置。
Hope this helps.
希望这可以帮助。
回答by Teddy mauve
Not sure I understand "exactly" what you're after but if you want to drag an image view into the controller, and that image view to automatically fill the whole view controller background: just drag the image view component into the view controller window.
不确定我“确切地”理解您所追求的是什么,但是如果您想将图像视图拖到控制器中,并且该图像视图会自动填充整个视图控制器背景:只需将图像视图组件拖到视图控制器窗口中即可。
Thing is: if you see a blue bounds line around the view controller, before you drag, the image view won't fill the view controller.
事情是:如果您在视图控制器周围看到蓝色边界线,则在拖动之前,图像视图将不会填充视图控制器。
You need to click anywhere on the background in the view controller pane, so the blue bounds line disappears, only then, will the image view you're dragging in, fill the whole controller background automatically.
您需要单击视图控制器窗格中背景上的任意位置,这样蓝色边界线就会消失,只有这样,您拖入的图像视图才会自动填充整个控制器背景。
If you find, using previews, your pics are getting squished or stretched, then, just go to the "Resolve Auto Layout Issues" menu, and click "Add missing constraints" - then all should resize to look ok on all screens.
如果您发现,使用预览,您的图片被压扁或拉伸,那么,只需转到“解决自动布局问题”菜单,然后单击“添加缺少的约束” - 然后所有内容都应调整大小以在所有屏幕上看起来都不错。