创建布局限制编程 [英] Creating layout constraints programmatically

查看:259
本文介绍了创建布局限制编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很多人都已经问吨这个问题,但即使有答案,我不能使它工作。

I know that a lot people already asked tons of questions about this, but even with the answers I can't make it work.

当我处理与故事板的限制,很容易,但code我有一个困难时期。
我尝试,例如,有一个停留在右侧,有根据屏幕方向屏幕的高度视图。这是我的code:

When I'm dealing with constraints on storyboard, it's easy but in code I have a hard time. I try, for example, to have a view that stays on the right side and has the height of the screen according the screen orientation. This is my code:

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 748)];
myView.backgroundColor = [UIColor redColor];
[self.view addSubview:myView];
[self.view addConstraints:[NSLayoutConstraint
    constraintsWithVisualFormat:@"V:|-[myView(>=748)]-|"
    options:0 metrics:nil
    views:NSDictionaryOfVariableBindings(myView)]];

有不满足某些约束。我不明白什么是错的。另外,为什么我不能使用像一个属性 self.myView ,而不是像一个局部变量 MyView的

It doesn't satisfy some constraints. I don't see what is wrong. Also, why can't I use a property like self.myView instead of a local variable like myView?

推荐答案

当在code。使用自动布局,设置框架不执行任何操作。所以,你指定的200宽度以上观点的事实,并不时在其上设置的限制意味着什么。为了设置为明确的视图的约束,它需要四样东西:一个X位置,Y位置,宽度和高度为任何给定的状态

When using Auto Layout in code, setting the frame does nothing. So the fact that you specified a width of 200 on the view above, doesn't mean anything when you set constraints on it. In order for a view's constraint set to be unambiguous, it needs four things: an x-position, a y-position, a width, and a height for any given state.

目前在上面的code,你只有两个(高度,相对于上海华和y位置,相对于上海华)。除了这个,你有两个所需的限制,可以根据不同的视图的上海华的制约因素是如何设置冲突。的如果的的上海华是有要求的约束,指定它的高度比748少一些价值,你会得到一个不可满足的约束的异常。

Currently in the code above, you only have two (height, relative to the superview, and y-position, relative to the superview). In addition to this, you have two required constraints that could conflict depending on how the view's superview's constraints are setup. If the superview were to have a required constraint that specifies it's height be some value less than 748, you will get an "unsatisfiable constraints" exception.

您已经设置视图的宽度设置约束之前,这意味着什么。它甚至不会拿旧框架考虑,并会计算基于所有,该公司已经对这些意见规定的约束的新的帧。 CGRectZero 或只是的init :当在code自动版式处理,我通常只是用 initWithFrame创建一个新的视图

The fact that you've set the width of the view before setting constraints means nothing. It will not even take the old frame into account and will calculate a new frame based on all of the constraints that it has specified for those views. When dealing with autolayout in code, I typically just create a new view using initWithFrame:CGRectZero or simply init.

要为你创造你的问题口头说明的布局要求的约束集,你需要一些横向的约束添加到约束的宽度和X位置,以便给一个完全指定的布局:

To create the constraint set required for the layout you verbally described in your question, you would need to add some horizontal constraints to bound the width and x-position in order to give a fully-specified layout:

[self.view addConstraints:[NSLayoutConstraint
    constraintsWithVisualFormat:@"V:|-[myView(>=748)]-|"
    options:NSLayoutFormatDirectionLeadingToTrailing
    metrics:nil
    views:NSDictionaryOfVariableBindings(myView)]];

[self.view addConstraints:[NSLayoutConstraint
    constraintsWithVisualFormat:@"H:[myView(==200)]-|"
    options:NSLayoutFormatDirectionLeadingToTrailing
    metrics:nil
    views:NSDictionaryOfVariableBindings(myView)]];

口头描述此布局如下开始与垂直约束:

Verbally describing this layout reads as follows starting with the vertical constraint:

MyView的将填补它的父的身高与顶部和底部填充
  等于标准的空间。 MyView的的上海华有一个最低高度
  的748pts。 MyView的宽度是200pts并有右填充等于
  标准对空间它的父。

myView will fill its superview's height with a top and bottom padding equal to the standard space. myView's superview has a minimum height of 748pts. myView's width is 200pts and has a right padding equal to the standard space against its superview.

如果您只想视图填满整个上海华的身高没有制约上海华的身高,那么你将只是省略(大于= 748)在参数视觉格式的文本。如果你认为(大于= 748)参数是必需的,给它一个高度 - 你不必在这种情况下:寄希望于视图使用的上海华的边缘酒吧( | )或空间(酒吧| - - | )语法,你是给你的视图的y位置(寄托在一个边缘的观点),与高度y位置(寄希望于两边的视图),因此满足您的约束为视图设置

If you would simply like the view to fill the entire superview's height without constraining the superview's height, then you would just omit the (>=748) parameter in the visual format text. If you think that the (>=748) parameter is required to give it a height - you don't in this instance: pinning the view to the superview's edges using the bar (|) or bar with space (|-, -|) syntax, you are giving your view a y-position (pinning the view on a single-edge), and a y-position with height (pinning the view on both edges), thus satisfying your constraint set for the view.

在关于你的第二个问题:

In regards to your second question:

使用 NSDictionaryOfVariableBindings(self.myView)(如果你有对MyView的一个属性设置),喂养到VFL使用 self.myView 在VFL的文字,你可能会在自动布局尝试来分析你的VFL文本得到一个异常。它在字典中的键点号和系统试图用做 valueForKeyPath:。 <一href=\"http://stackoverflow.com/questions/13045178/nsinvalidargumentexception-reason-unable-to-parse-constraint-format\">See这里类似的问题和答案。

Using NSDictionaryOfVariableBindings(self.myView) (if you had an property setup for myView) and feeding that into your VFL to use self.myView in your VFL text, you will probably get an exception when autolayout tries to parse your VFL text. It has to do with the dot notation in dictionary keys and the system trying to use valueForKeyPath:. See here for a similar question and answer.

这篇关于创建布局限制编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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