Xcode 6:如何在所有设备上填充图像视图?自动布局不起作用? [英] Xcode 6: How to make an image view fill the screen on all devices? Auto layout not working?
问题描述
所以我有一个图像视图和一个按钮。我需要在所有设备上将图像视图设置为正确的尺寸(不要看起来压扁或拉伸),更重要的是我需要将其视为屏幕的大小。我需要按钮才能在所有设备上保持相同的位置。
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.
回到Xcode 5我记得自动布局修复了这个问题,但现在不起作用了。我查看了 https://www.youtube.com/watch?v=G53PuA_TlXk 和尝试控制 - 拖动到视图控制器并水平和垂直居中视图,但我无法将其缩放到屏幕的大小。视频没有解决这个问题。
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_TlXk and 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.
在控制拖动以保持图像视图缩放时,是否需要选择特定选项?我该怎么办?
Is there a specific option I need to choose when control-dragging to keep the image view scaled? What should I do?
推荐答案
你需要做两件事:
- 正确设置约束
- 选择所需的缩放类型。
设置约束:
Setting Up Your Constraints:
- 拖出图像视图并将其大致放在您的观点的中心。
- 将您的尺寸等级设为w 任意 h 任意。
- 单击屏幕底部的图钉 | - [] - | 。
- 打开所有四个橙色工字梁。将四个常量设置为零。
- 取消选中约束到边距复选框。
- 单击添加4个约束
- Drag out an Image View and place it roughly in the center of your view.
- Set your size class to wAny hAny.
- 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 margins checkbox.
- Click Add 4 constraints
选择缩放:
- 点击您添加到视图控制器的图像视图。
- 在右侧的属性检查器中,选择您的图像。
- 将查看的模式设置为 Aspect Fill (根据需要填充整个屏幕顶部/底部或左/右裁剪)或 Aspect适合(信箱图像,以便整个图像显示未剪切)。
- Click on the Image View you added to your View Controller.
- In the Attributes Inspector on the right, choose your image.
- Set the Mode of the View to 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).
您可以查看约束那是为你创造的。在文档大纲中,您应该在图像视图下看到4个约束:
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 Inspector on the right. Here are the 4 that I created:
这篇关于Xcode 6:如何在所有设备上填充图像视图?自动布局不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!