如何在XCode 11中更改图像的位置以进行纵向或水平对齐

问题描述 投票:0回答:1

我正在尝试从水平方向观看时填充屏幕的右侧到纵向观看时占据上半部分的图像。

我遵循了很多指南和points on here,但是它们都与旧版本的XCode有关,这些旧版本似乎具有XCode 11所不具备的功能-such as selecting constant-或只是行不通。

我完全困惑于它应该如何工作,因为当我添加“ Vtrait for traits”时,此举适用于所有设备。我敢肯定,我必须考虑这个问题,它一定更简单吗?

我在支持以上版本的iOS 13.x上使用XCode 11.4。

enter image description here

ios xcode autolayout constraints screen-rotation
1个回答
0
投票

[通过添加新的视图控制器开始-以纵向方向(w C h R)使用iPhone 8布局:

enter image description here

添加新的UIImageView并将其放置在顶部,高度大约为1/4(无关紧要):

enter image description here

添加顶部/前导/尾随约束:

enter image description here

添加比例高度约束,为0.25:

enter image description here

现在更改景观方向视图(w C h C):

enter image description here

它看起来像这样(如预期):

enter image description here

单击Vary for Traits,然后选择Height,因为我们从(w C h R)更改为(w C h C):

enter image description here

查看“大小”检查器中的约束,然后选择This Size Class

enter image description here

删除超前和比例高度约束:

enter image description here

并添加底部约束和比例宽度(0.5)约束:

enter image description here

然后单击Done Varying。现在看起来像这样:

enter image description here

[注意,我们有2个“灰显”(非活动)约束...如果切换回以人像方向查看,这两个约束将变为“活动”,而这两个(w C h C)约束将显示为“无效”。

由于应用可以在部分屏幕上运行(iPad多任务处理),因此建议您不要再以纵向和横向进行思考。而是从大小分类/特征方面考虑。

如果您的设计允许,您还可以添加(w R h R)性状变异。

现在,如果您really想要纵向vs横向,则需要使用一些代码。为“高/窄”布局定义一组约束,为“宽/短”布局定义另一组约束。然后实现viewWillTransition(to size: ...并根据“按大小” width > heightheight > width激活/停用适当的约束。

© www.soinside.com 2019 - 2024. All rights reserved.