简体   繁体   English

Xcode故事板约束和自动布局

[英]Xcode storyboard constraints and auto layout

I'm working on an app at the moment and one thing that I've been struggling with for the past two weeks has been auto layout. 目前,我正在开发一个应用程序,过去两周我一直在努力的一件事是自动布局。 I'm new to iOS programming so I'm not exactly sure the best way to explain my problem but here goes. 我是iOS编程的新手,所以我不确定要解释我的问题的最佳方法,但是这里有。

I'm making a piano app and the way it works is that my keyboard is just a bunch of buttons laid out next to each other. 我正在制作一个钢琴应用程序,其工作方式是我的键盘只是一排彼此相邻布置的按钮。

在此处输入图片说明

These buttons represent the keys. 这些按钮代表按键。 The buttons have a background image of an image of a regular piano key. 按钮具有普通钢琴键的图像的背景图像。 Now my problem is that I have a black above the keys and whenever I try to use the pin tool to create constraints for my piano keys, as soon as i add the constraints everything gets pushed up and my keys get stretched vertically. 现在我的问题是,琴键上方有一个黑色,每当我尝试使用销钉工具为我的钢琴琴键创建约束时,一旦添加约束,所有内容都会被推上,琴键会垂直拉伸。

在此处输入图片说明

在此处输入图片说明

I've been working hours non stop this past week not knowing what's wrong. 过去一周我一直在不停地工作,不知道出什么问题了。 I've followed manny tutorials but the one's I follow are very basic and at most have a few labels or buttons separated by a ton of space. 我遵循过许多教程,但我遵循的教程非常基础,最多只有几个标签或按钮,它们之间有大量空格隔开。 I'm not sure if it's because my buttons are not spaced out enough or what but it's just extremely frustrated. 我不确定这是因为我的按钮没有足够的间距还是什么,但是只是非常沮丧。 Any help, or guides/tutorials would be super helpful... 任何帮助或指南/教程都将非常有帮助...

Also I've tried checking the boxes that say to width and height to maintain the width and height however that just makes the keys either too huge or too small on some devices. 我也尝试过检查宽度和高度的复选框以保持宽度和高度,但是这只会使某些设备上的按键太大或太小。 Is there a way to stretch these keys so they adapt to their device? 有没有办法拉伸这些按键,使其适应设备? I've heard of intrinsic size content but don't know how that would apply with over 24 buttons... 我听说过固有尺寸内容,但不知道超过24个按钮如何应用...

Edit: I tried a stack view but all my white keys got spaced out and went out of the storyboard: 编辑:我尝试了一个堆栈视图,但是我所有的白色键都被间隔开了,离开了故事板:

在此处输入图片说明

Here's what I would do. 这就是我要做的。 Let's ignore the black bar at the top for a moment and concentrate solely on the keys. 让我们暂时忽略顶部的黑条,只专注于按键。 And for a moment let's ignore the black keys. 暂时让我们忽略黑键。 So let's imagine we have 14 white key views, and they are to fill the screen regardless of its size. 因此,让我们想象一下,我们有14个白键视图,无论它们的大小如何,它们都会填满整个屏幕。

That's easy. 这很容易。 Start with one white key view. 从一个白色按键视图开始。 Pin it to the top, left, and bottom of the superview, and set its width arbitrarily to, say, 10. Now just keep adding white key views with their left pinned to the right of the previous view, their top and bottom pinned to the superview, and their width set equal to the first view's width . 将其固定在超级视图的顶部,左侧和底部,并将其宽度任意设置为10。现在,只需继续添加白色关键视图,并将其左侧固定在上一个视图的右侧,将顶部和底部固定在超级视图,其宽度设置为等于第一个视图的width Finally, pin the 14th key view's right to the right of the superview. 最后,将第14个关键视图的右边固定在超级视图的右边。

At that point, of course, Interface Builder will balk, because the widths do not all add up to make it possible to pin the leftmost key to the left and the rightmost key to the right. 当然,到那时,Interface Builder会大吃一惊,因为宽度的总和并没有全部累加,因此可以将最左边的键固定在左侧,而将最右边的键固定在右侧。 But now you remove the width constraints on the first key! 但是现在您删除了第一个键的宽度限制! Presto, the keys adjust themselves to fill the total width of the screen. Presto,按键会自行调整以填满屏幕的整个宽度。

Here's the result in Interface Builder on my screen. 这是我屏幕上的Interface Builder中的结果。 I've allowed a little extra space and I've colored the white keys green for clarity. 我留出了一些额外的空间,并且为了清晰起见,我将白色按键涂成了绿色。 I created an IBDesignable custom UIView subclass so as to simulate the roundy shape of the bottom end of the keys. 我创建了一个IBDesignable自定义UIView子类,以便模拟键底端的圆形形状。

在此处输入图片说明

The black keys are similar except that you will need three invisible "spacer" black keys to dictate the gaps in the series. 黑键类似,除了您需要三个不可见的“空格”黑键来指示系列中的间隙。 Here's my Interface Builder with the black keys added: 这是添加了黑键的我的Interface Builder:

在此处输入图片说明

Here's the app running on the iPad simulator: 这是在iPad模拟器上运行的应用程序:

在此处输入图片说明

Here it is running on the iPhone 5s simulator: 它在iPhone 5s模拟器上运行:

在此处输入图片说明

That shows, I think, that this is a viable strategy. 我认为,这表明这是一种可行的策略。 It's just a matter of minor tweaks from this point to get the exact look you want. 从现在开始,只需进行一些细微调整即可获得所需的外观。

Buttons will be easier to use than views when it comes to triggering actions so stick with buttons. 在触发动作方面,按钮比视图更易于使用,因此请坚持使用按钮。 We will constrain their sizes so they behave. 我们将限制它们的大小,以便它们表现出来。

Start by getting rid of the black keys until the white keys are all set. 从摆脱黑键开始,直到所有白键都已设置。

Pin all of the white keys to the top and bottom of the screen. 将所有白色键固定在屏幕的顶部和底部。

Set them all to have the same width. 将它们全部设置为相同的宽度。

Pin the first key to the leading edge and the last key to the trailing edge of the superview. 将第一个键固定到超级视图的前沿,最后一个键固定到后视图。

Select all the white keys except the last one and pin the right side with the spacing at 0. 选择最后一个按键以外的所有白色按键,并将右边的空格固定为0。

That should make it so that all the white keys fill the screen with no space around the edges or between them. 这样就可以使所有白色键填满整个屏幕,在边缘周围或边缘之间没有任何空间。

Use the assistant editor in the storyboard and select preview from it. 在情节提要中使用助手编辑器,然后从中选择预览。 Add a few different screen sizes and turn them to landscape. 添加一些不同的屏幕尺寸并将其变为横向。 It may not look exactly the way it will on a device but it should be pretty close. 它可能看起来不完全像在设备上的样子,但是应该非常接近。

Once the white keys look right add the black keys into the mix. 白色键看起来正确后,将黑色键添加到混合中。

Start with just one black key and drag to resize it to the proper size in the storyboard. 仅从一个黑键开始,然后拖动以在情节提要中将其调整为适当的大小。 Once you have that size right add an aspect ratio constraint so you only have to worry about adjusting the width and not the height. 正确设置尺寸后,添加一个宽高比约束,因此您只需要担心调整宽度而不是高度。

Temporarily set the label of the black key to "b" or some other single character, we will need it later. 暂时将黑键的标签设置为“ b”或其他某个单个字符,稍后我们将需要它。

You will need to do a tiny bit of math to get the black key to be the right width on all screens… Select the black key and look in the size inspector for it width, do the same for the superview. 您将需要做一点点的数学运算,以使黑键在所有屏幕上都具有正确的宽度...选择黑键,然后在大小检查器中查找其宽度,对超级视图执行相同的操作。 Once you have those numbers divide the black key width by the superview width. 一旦有了这些数字,就将黑键宽度除以超级视图宽度。 make note of the result as you will need it in a moment. 请记下结果,因为稍后您会需要它。

Now that you know the percentage of the screen width the black key takes up select the black key and the superview. 现在您知道了黑键占据的屏幕宽度百分比,选择黑键和超级视图。 Set them to have the same width. 将它们设置为相同的宽度。 (This may make the black key get crazy big, we will fix that in a moment) (这可能会使黑键变得疯狂起来,我们稍后会修复)

Select just the black key, go to the size inspector, and double click on the constraint that makes its width match the superview. 仅选择黑键,转到大小检查器,然后双击使其宽度与父视图匹配的约束。

That will select the constraint itself. 那将选择约束本身。 In its attributes set the multiplier to be the same as the decimal number you got by dividing the black key width by the screen width. 在其属性中,将乘数设置为与通过将黑键宽度除以屏幕宽度而获得的十进制数相同。

That takes care of the size of the black keys, now to deal with positioning them. 这就照顾了黑键的大小,现在可以处理它们的位置。

First duplicate the black key you have created until you have all that you need. 首先复制您创建的黑键,直到拥有所需的全部。

Select all the black keys and pin them to the top of the superview. 选择所有黑键,然后将其固定在超级视图的顶部。

Manually drag all the black keys into the right places (you don't need to be precise just in the right area). 手动将所有黑键拖到正确的位置(您不必在正确的区域内保持精确)。

The final bit of alignment we need to do is to make it so that the center of each black key is over the line between white keys… 我们需要做的最后对齐工作是使每个黑键的中心都在白键之间的直线上。

Select a black key and either control or right click and drag from it to the white key on its right side. 选择一个黑键,然后控制或单击鼠标右键并将其从其拖到右侧的白键。 Select horizontal spacing from the menu that appears. 从出现的菜单中选择水平间距。

In the size inspector double click on that new constraint (it should have a negative number). 在大小检查器中,双击该新约束(该约束应为负数)。

In the attributes for the constraint you should see that it has a first item and second item. 在约束的属性中,您应该看到它具有第一项和第二项。 One of those items is "b" (or whatever label you used for the black key earlier). 这些项目之一是“ b”(或您先前用于黑键的任何标签)。 If you click on the popup menu you should see trailing is checked. 如果单击弹出菜单,应该会看到尾部已选中。 Select Center X instead. 而是选择CenterX。 Finally set the constant to 0. 最后将常数设置为0。

That should lock that black key into the proper size and place. 那应该将黑键锁定到适当的大小和位置。 Now repeat the process for the rest of the black keys. 现在,对其余的黑键重复该过程。

Once that is done you can get rid of the labels and make sure all the images are properly set. 完成后,您可以去除标签,并确保正确设置了所有图像。

It isn't a quick process but it gets easier over time. 这不是一个快速的过程,但是随着时间的推移它会变得更加容易。

If the white keys end up with space on some screens try making sure they are set to scale to fill (hopefully that doesn't mess up the bottom curves too much). 如果白色键在某些屏幕上最终以空格结束,请尝试确保将它们设置为缩放以填充(希望这样不会使底部曲线混乱太多)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM