Xcode如何在Autolayout中进行灵活约束 [英] Xcode how to make flexible constraint in Autolayout

查看:171
本文介绍了Xcode如何在Autolayout中进行灵活约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我被困在自动布局问题中

hello everyone I've get stuck in Auto-layout issues

我有一个登录情节提要,其中包含三个项目Image-view,Stack-view和Button,如下面的第一个图像

I have login storyboard that contains three Items Image-view , Stack-view , Button like the first image below

我尝试使用两种方法添加约束:

I've tried to add constraints using two ways:

我向图像"视图添加固定"约束的第一种方法,如下面的照片1所示,但是当我更改为横向时,图像"作为下面的第二张照片消失了.
请注意,我没有任何警告

The first way I've added "pinned" constraint to Image-view as shown in Photo1 below but when I've changed to landscape the Image was disappeared as the second photo below.
please notice I haven't got any warnings

我尝试使用视图包装Image-view的第二种方法,然后我添加了 要查看固定"约束 最后,我添加了固定宽度&图像视图和水平&的高度垂直放置在容器中,但我得到的结果是第三张和第四张照片

The second Way I've tried by wrap the Image-view using view then i added "Pinned" constraint to view finally i added fixed width & height to Image-view and Horizontal & vertical in container but I've got this result as third and fourth photo

我应该怎么做才能使我的图像视图适应所有屏幕?

what should i do to make my Image-view Adaptive for all Screens ?

谢谢

照片1 照片2 Photo3 Photo4

推荐答案

与更改为横向屏幕高度时一样.使用自动版式进行设计的问题在于它不适用于横向和纵向模式,因为会为您的版式赋予固定的宽度.

As when changing to landscape screen height changing a lot. the problem with your design with Autolayout is it's not adaptive with landscape and portrait mode because you are give fixed hight and width to your layout.

使您的图像视图适用于所有屏幕,使您的图像视图高度变为超级视图 高度 percentage 为单位,因此在横向模式下,高度会改变,图像视图仍会以与 percentage完全相同的方式从 superview height 获得 height .

to make your image-view Adaptive for all screens give your image-view height to superview height in percentage so when in landscape mode height will change your image-view will still gets the height from superview height in exact same percentage.

看一下这个演示,希望对您有所帮助. 自动布局

have a look at this demo hope this will help. Autolayout

  • 首先将UIImageView放到ViewController中.
  • 赋予它最大的限制,并将其水平对齐到其SuperView. 到现在为止,只需要高度和宽度.
  • 然后 Ctrl ImageView 拖动到自己选择宽高比.给 方面 1:1的比例(它只是告诉故事板如果高度或 宽度可用,但我希望另一个与宽度相同 给定的高度应与宽度相同,反之亦然高度) 您的个人资料ImageView的完美正方形.

  • First drop UIImageView into ViewController.
  • Give it a top constraint and Align it horizontally to it's SuperView. As now it's only required height and width.
  • Then Ctrl drag from ImageView to itself and select aspect ratio. give a aspect ration 1:1 (it's just telling storyboard if any of height or width is available i want the other to be the same like if width is given then height should be the same as width, vice versa for height) as you may want perfect square shape for your profile ImageView.

现在 Ctrl ImageView 拖动到 SuperView ,然后选择等高.

Now Ctrl drag from ImageView to SuperView and select Equal height.

  • 现在,您的ImageView已将高度设置为 SuperView高度. 给您的ImageView 比例高度(百分比,长宽比 Ration或Multipiler)以 SuperView 的价格提供给您 如此.
  • As now your ImageView got the height to SuperView Height. give your ImageView proportional height (percentage, Aspect Ration or Multipiler) to SuperView how much you wanted to give like so.

最后,您的UIImageView的高度将与SuperView的高度成比例,反之亦然.

这篇关于Xcode如何在Autolayout中进行灵活约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆