简体   繁体   English

创建基于百分比的iOS布局

[英]Creating a percentage based iOS layout

I'm trying to replicate a layout that I currently have in an Android application, but I don't know how to go about it in iOS especially because of the tallness of the iPhone 5. 我正在尝试复制当前在Android应用程序中拥有的布局,但是由于iPhone 5的高度,我不知道如何在iOS中使用它。

I know how to explain this in "Android" terms but I've been trying for the past few days to do this in iOS but I can't quite get it to work. 我知道如何用“ Android”术语来解释这一点,但是过去几天我一直在尝试在iOS中做到这一点,但我还无法完全正常工作。

Best way to explain it: 最好的解释方式:

  1. I want two layouts. 我想要两种布局。 The top layout must take up 40% and the bottom must take up 60%. 顶部布局必须占40%,底部布局必须占60%。
  2. In the top layout, they must be three buttons that fill up all space possible (Essentially 1/3 of the space) 在顶部布局中,它们必须是三个按钮,以填满所有可能的空间(基本上是空间的1/3)
  3. In the bottom layout, I want an imageView, and then a textView on top of that. 在底部布局中,我需要一个imageView,然后是一个textView。

This is a paint mockup. 这是一个油漆模型。 Is this possible to do in iOS? 这可以在iOS中完成吗? I feel that layouts are much harder to create than android. 我觉得布局比android更难创建。

在此处输入图片说明

Using Xcode 6.0, you can now specify proportional width or height in Interface Builder. 使用Xcode 6.0,您现在可以在Interface Builder中指定比例宽度或高度。 Steps for percentage height from superview: 从超级视图获取百分比高度的步骤:

While both the child view and its superview are selected, add an "equal height" constraint (or "equal width" if you wish to have a proportional width). 选中子视图及其父视图时,添加“等高”约束(如果希望具有比例宽度,则添加“等宽”)。

在此处输入图片说明

Then change the "multiplier" of the constraint you just added to the proportion you need. 然后将刚添加的约束的“乘数”更改为所需的比例。 For example, for 50%, change it to 2. 例如对于50%,将其更改为2。

If you like to specify the inner view as percentage of the superview, you can reverse the items in the constraint: 如果要将内部视图指定为超级视图的百分比,则可以反转约束中的项目:

在此处输入图片说明

Now you can use a multiplier of 0.5 (or any other proportion you need): 现在,您可以使用0.5的乘数(或您需要的任何其他比例):

在此处输入图片说明

In your specific case, you can define an "equal height" constraint between the 2 child views, and change the multiplier to 1.5 (the bottom being 1.5 the size of the top) or 0.6667 if the items are reversed. 在您的特定情况下,您可以在2个子视图之间定义一个“等高”约束,并将乘数更改为1.5(底部为顶部大小的1.5)或0.6667(如果反转)。

Contrary to the other answers, I think you should at least consider the auto layout system. 与其他答案相反,我认为您至少应该考虑自动布局系统。 It was created to make it easier to build predictable layouts like the one you've described. 它的创建是为了使您可以轻松构建可预测的布局(如您所述的布局)。 Autolayout is ruled by constraints that you put on the views in the layout. 自动布局放置在布局视图中的约束的约束 You can create those constraints visually or programmatically. 您可以通过视觉或编程方式创建这些约束。 Visually, your first view would look something like this: 在视觉上,您的第一个视图将如下所示:

视觉约束

The blue lines you see there are a number of constraints that specify the inter-button spacing, the space around the buttons, and the heights and widths of the buttons. 看到的蓝线有很多约束,这些约束指定按钮之间的间距,按钮周围的空间以及按钮的高度和宽度。 You can see a couple constraints that have a = on them -- I selected all three buttons and gave them an "equal height" constraint. 您会看到一对在其上带有=约束-我选择了所有三个按钮,并为其赋予了“等高”约束。

There's a nice visual format syntax that lets you describe constraints as strings. 有一种很好的视觉格式语法 ,可用于将约束描述为字符串。 Take a minute to look at the linked document -- it won't take much longer than that to learn enough that you can read the strings. 花一点时间看一下链接的文档-只需花费很长时间即可了解到足以阅读字符串的知识。 For example, your top layout can be specified like this: 例如,可以这样指定顶部布局:

V:[button1]-[button2(==button1)]-[button3(==button1)]

The parenthetical ==button1 tells the layout system to make button2 and button3 the same height as button1 . 括号==button1告诉布局系统使button2button3的高度与button1相同。 The - indicates that the standard spacing should be used between the buttons; -表示按钮之间应使用标准间距; you can specify a different spacing if you want. 您可以根据需要指定其他间距。 For 10 point spacing, do this: 对于10点间距,请执行以下操作:

V:|-10-[button1]-10-[button2(==button1)]-10-[button3(==button1)]-10-|

Once you have such a string, you can turn it into a constraint using the method: +[NSLayoutConstraint constraintsWithVisualFormat:options:metrics:views:] 一旦有了这样的字符串,就可以使用以下方法将其变成约束: +[NSLayoutConstraint constraintsWithVisualFormat:options:metrics:views:]

Some constraints can't be specified either visually or with the strings described above. 不能通过视觉或上述字符串指定某些约束。 Chief among these are those where you want to set a constant (but unequal) relationship between two views, as with your top and bottom layouts. 其中最主要的是您想要在两个视图之间设置恒定(但不相等)关系的视图,就像您的顶部和底部布局一样。 You want one of those to take up 40% of the available vertical space, and the other to take 60%. 您希望其中一个占据40%的可用垂直空间,而另一个占据60%的垂直空间。 Do this using the method: +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] . 使用以下方法执行此操作: +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] For example: 例如:

NSLayoutConstraint *c = [NSLayoutConstraint constraintWithItem:bottomView
                                                     attribute:NSLayoutAttributeHeight
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:topView
                                                    multiplier:1.5
                                                      constant:0];

That gives you a constraint that sets the height of bottomView to 1.5 times the height of topView (which is what you want, since 60/40 = 1.5). 这使您设置的高度约束bottomView到高的1.5倍topView (这是你想要的,因为60/40 = 1.5)。

If you create constraints programmatically, you can add them to the appropriate view when you create (or load) the view hierarchy. 如果以编程方式创建约束,则可以在创建(或加载)视图层次结构时将它们添加到适当的视图中。 Your view controller's -viewDidLoad method is a fine place to do that. 您的视图控制器的-viewDidLoad方法是执行此操作的好地方。

I don't know about using autolayout as I dont use it, but in code without it, you could create two UIViews and set their frames to: 我不知道使用自动布局,因为我不使用它,但是在没有它的代码中,您可以创建两个UIViews并将它们的框架设置为:

CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 0.4f);
CGRectMake(0, self.view.frame.size.height * 0.4f, self.view.frame.size.width, self.view.frame.size.width * 0.6f);

And then in the top view, you can add the buttons with frames (assuming the view is called view1 ): 然后在顶视图中,可以添加带有框架的按钮(假设该视图称为view1 ):

CGRectmake(0, 0, self.view.frame.size.width, view1.frame.size.height * (1.0f/3.0f));
CGRectmake(0, view1.frame.size.height * (1.0f/3.0f), self.view.frame.size.width, view1.frame.size.height * (1.0f/3.0f));
CGRectmake(0, view1.frame.size.height * (2.0f/3.0f), self.view.frame.size.width, view1.frame.size.height * (1.0f/3.0f));

You need to add a couple of constraints to make it work. 您需要添加一些约束以使其正常运行。 So here's a brief description of what you need: 因此,这里是您需要的简要说明:

  1. You will need horizontal spacing constraints. 您将需要水平间距约束。 One if for the top view, because it has zero distance to the top. 如果是顶视图,则为一个,因为它到顶的距离为零。 Another one for the bottom view, because its distance to the bottom is zero. 另一个用于底视图,因为它到底部的距离为零。 One more constraint between the two views, because they have also zero spacing between each other. 两个视图之间还有一个约束,因为它们之间的间距也为零。

  2. The same principle will be applied to the buttons inside the top view. 相同的原理将应用于顶视图内的按钮。 Put your buttons with the correct size inside the top view and add similar constraints, but now you have three elements (instead of two). 将尺寸正确的按钮放在顶视图内,并添加类似的约束,但是现在您有了三个元素(而不是两个)。 So constraints for zero spacing to the top, bottom and between buttons. 因此限制了顶部,底部和按钮之间的零间距。

When you add components to your view, it will create a few constraints for you. 当您向视图中添加组件时,它将为您创建一些约束。 Adding one one top and the other on bottom, it will create both spacing to the top and spacing between each other. 在顶部添加一个,在底部添加另一个,这将同时创建顶部间距和彼此之间的间距。 To edit them, just go to inspector, fifth tab (ruler) and you will see the list of constraints. 要编辑它们,只需转到检查器的第五个选项卡(规则),您将看到约束列表。 Lastly, you may want to try to constraints menu (I don't know if there's a well-known name for this). 最后,您可能想尝试限制菜单(我不知道是否有一个众所周知的名称)。 It's on the bottom-right corner of the Interface Builder canvas. 它位于Interface Builder画布的右下角。 See the image: 见图片:

在此处输入图片说明

Please, let me know if you need further assistance. 请让我知道是否需要进一步的帮助。

This can be done automatically when using Storyboards (you might have to change a setting or two here and there). 使用情节提要板时,此操作可以自动完成(您可能需要在此处或此处更改一个或两个设置)。 When you create your GUI, you can toggle between the screen sizes (3.5 and 4 inch) to make sure that it will look good on both. 创建GUI时,可以在屏幕尺寸(3.5英寸和4英寸)之间切换,以确保在两个屏幕上均显示良好。 See the answer to this question . 请参阅此问题的答案

You could also see this tutorial . 您还可以看到本教程 That should give you an idea on how to work with the GUI layouts. 那应该给您一个关于如何使用GUI布局的想法。

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

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