简体   繁体   English

如何在Xcode的Interface Builder中使用约束/自动布局来正确缩放图像和字体?

[英]How do I use constraints / auto layout in Xcode's Interface Builder to properly scale images and fonts?

I'm trying to make a user interface in Xcode's Interface Builder such that: 我正在尝试在Xcode的Interface Builder中创建用户界面,例如:

  1. It scales views based on the width of the device changing (aspect scaling) 它根据设备宽度变化来缩放视图(方面缩放)
  2. Images are scaled by aspect ratio (no stretched or squashed images) 图像按宽高比缩放(没有拉伸或压缩的图像)
  3. Fonts are scaled linearly 字体线性缩放
  4. If something is centered horizontally in one design, it should be centered in all designs 如果某事物在一个设计中水平居中,则应在所有设计中居中

Simple goals, I think… I have accomplished all of this programmatically using custom code previously, but I'd rather do it the "right way" and design this properly in Interface Builder and let it handle all of the layout for me. 我认为简单的目标……我以前已经使用自定义代码以编程方式完成了所有这些工作,但是我宁愿以“正确的方式”做到这一点,并在Interface Builder中正确设计它,并让它为我处理所有布局。

Let's say I have designed in Interface Builder a screen for a small form-factor iPhone like this: 假设我在Interface Builder中为这样的小型iPhone设计了一个屏幕:

原始布局

Now say this look and feel I'm happy with for iPhone 4s devices and would just like to scale everything up proportionally to the width for newer, larger iPhone form factors. 现在说一下这种外观,我对iPhone 4s设备感到满意,并且只想按比例将所有内容按比例放大,以适应更新,更大的iPhone尺寸。

If I were to use the older "springs and struts" method, this almost gives me what I want on a larger form factor... except that everything stretches height-wise (not simply based on aspect ratio of original design's width to height.) You can notice particularly the "logo image" is stretched too tall. 如果我要使用较旧的“弹簧和支柱”方法,那么这几乎可以在更大的外形尺寸下满足我的要求……除了所有内容都沿高度方向拉伸(不只是基于原始设计的宽高比)。 )您会特别注意到“徽标图像”拉伸得太高。 And the fonts aren't being scaled up on labels or buttons either. 而且字体也不会在标签或按钮上按比例放大。

无需更改即可缩放

I've also tried using "constraints", particularly just tackling the issue of image scaling proportionally, basing it on the aspect ratio of the container, and am not having any luck whatsoever. 我还尝试过使用“约束”,特别是仅按比例处理图像缩放问题,并基于容器的纵横比进行处理,而没有任何运气。

Here I reset the springs and struts to their defaults, and add constraint to the original design's logo image: 在这里,我将弹簧和支柱重置为其默认值,并为原始设计的徽标图像添加了约束:

具有宽高比限制的原始徽标

Looking at the larger form factor, nothing has in fact changed: 从更大的尺寸来看,实际上没有任何变化:

具有长宽比的更大尺寸

(And yes, despite the fact there's layout errors reported, "fixing" them in any way has no net effect on the scaling / lack of scaling in the image.) (是的,尽管报告了布局错误,但是以任何方式“修复”它们都不会对图像的缩放/缺乏缩放产生实际影响。)

So just to restate-- how do I do the following with Xcode / Interface Builder constraints? 因此,只需重申一下-如何使用Xcode / Interface Builder约束执行以下操作?

  1. It scales views based on the width of the device changing (aspect scaling) 它根据设备宽度变化来缩放视图(方面缩放)
  2. Images are scaled by aspect ratio (no stretched or squashed images) 图像按宽高比缩放(没有拉伸或压缩的图像)
  3. Fonts are scaled linearly 字体线性缩放
  4. If something is centered horizontally in one design, it should be centered in all designs 如果某事物在一个设计中水平居中,则应在所有设计中居中

Thanks in advance. 提前致谢。

  1. It scales views based on the width of the device changing (aspect scaling) 它根据设备宽度变化来缩放视图(方面缩放)
  2. Images are scaled by aspect ratio (no stretched or squashed images) 图像按宽高比缩放(没有拉伸或压缩的图像)

What you can do is to make width and height using aspect ratio. 您可以使用宽高比设置宽度和高度。

I have added a multiplier respect to superview 我为Superview添加了一个乘数方面

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

  1. Fonts are scaled linearly 字体线性缩放

I have set a label like this. 我已经设置了这样的标签。

在此处输入图片说明

And From attribute inspector i set like this: 从属性检查器,我这样设置:

在此处输入图片说明

  1. If something is centered horizontally in one design, it should be centered in all designs 如果某事物在一个设计中水平居中,则应在所有设计中居中

All views are center aligned. 所有视图均居中对齐。

在此处输入图片说明

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

相关问题 自 Xcode 8 以来的布局界面生成器约束问题 - Layout Interface Builder constraints issue since Xcode 8 Interface Builder中的相对自动布局约束 - Relative Auto Layout Constraints in Interface Builder 如何在Xcode的Interface Builder中一次编辑多个约束 - How to edit multiple constraints at once in Xcode's Interface Builder XCode Interface Builder自动布局问题 - XCode Interface Builder Auto Layout issue Xcode界面构建器自动布局垂直间距 - Xcode interface builder auto layout vertical spacing 有没有办法在Interface Builder中为Auto Layout Constraints添加标识符? - Is there a way to add an identifier to Auto Layout Constraints in Interface Builder? 如何在Interface Builder中使用自动布局创建键盘附件视图? - How do I create a keyboard accessory view with Auto Layout in Interface Builder? Swift 初学者 - 自动布局约束(Xcode Interface Builder)问题 - 'leading = label.leading' 与 'label.leading =leading' 相同 - Swift Beginner - Auto layout constraints (Xcode Interface Builder) question - is 'leading = label.leading' the same as 'label.leading = leading' 如何在Interface Builder和xcode中将表格和单元格背景更改为自定义图像? - How do I change the table and cell backgrounds to custom images in Interface Builder and xcode? 如何正确编辑自动布局约束? - How to edit Auto layout Constraints properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM