本文介绍了Xcode 6中的约束可以动态吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我很吃力,已经开始使用自动版式.适应起来并不比我担心的难.我对此有麻烦.

视图2是否有可能锚定"到视图1的底部.除非隐藏了视图1,在这种情况下它应锚定到包含视图的顶部?我将如何为此设置约束?

如果View1.hidden = NO:

如果View1.hidden =是:

解决方案

也许解决此问题的最简单方法是操纵约束优先级.

我不会详细介绍自动版式系统的工作方式-如果需要,请查看WWDC会话 202:iOS和OS X的自动版面设计 228 :这是2012年以来精通自动版式的最佳实践.此外,请查看 objc.io关于自动版式的精彩文章.

简而言之,可以为约束分配优先级.优先级由范围在[0,1000](含)之间的浮点数表示.特殊优先级1000表示必需";所有其他优先级是可选的.


我们如何使用它来提供帮助?在Interface Builder(IB)中,为蓝色视图的顶部创建两个不同的约束.第一个应该将蓝色视图的顶部与红色视图的底部相关联,且距离为0点-也就是说,蓝色视图的顶部应与红色视图的底部齐平.我们将这个约束称为未隐藏"约束.

第二个约束应从蓝色视图的顶部到超级视图的顶部,再次相距0点-也就是说,蓝色视图的顶部应与其超级视图的顶部齐平.我们将这个约束称为隐藏"约束.

如果您一直遵循,您将意识到除非红色视图的高度正好等于0,否则不可能同时满足这两个约束.调整红色视图的大小,只是隐藏或取消隐藏它),那么这些约束如何共存?答案在于优先事项.确保在IB中,未隐藏约束的优先级为950(请记住,此可选项,但优先级为950).但是,请将隐藏约束的优先级设置为小于950(例如,可能为450).

接下来,您需要创建针对这两个约束的出口(这样做的方法不在此答案的范围内–与在IB中为任何内容创建出口相同).我建议将它们命名为我在这里命名的名称.因此,在头文件中,您可能会看到以下内容:

...
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *unhiddenConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hiddenConstraint;
...

现在剩下的唯一事情就是在适当的时候更改这些约束的优先级,即隐藏或取消隐藏红色视图时的优先级.因此,在您的视图控制器中,添加如下方法:

- (void)setRedViewHidden:(BOOL)hidden {
    if (hidden) {
        self.unhiddenConstraint.priority = 450;
        self.hiddenConstraint.priority   = 950;
        self.redView.hidden = YES;
    } else {
        self.hiddenConstraint.priority   = 450;
        self.unhiddenConstraint.priority = 950;
        self.redView.hidden = NO;
    }
}

然后,每当要隐藏红色视图时,只需使用适当的参数调用此方法即可.在约束上设置优先级将隐式地将布局标记为脏,从而导致在UI循环的下一轮发生新的布局传递.

这不是Auto Layout引擎的初学者使用.如果您对此解决方案还有任何疑问,请告诉我.

I'm biting the bullet and have begun using auto layout. Not as difficult to get used to than I feared. I am having trouble with this though.

Is it possible for view 2 to 'anchor' to the bottom of view 1.. UNLESS view 1 is hidden, in which case it should anchor to the top of the containing view? How would I set constraints for this?

If View1.hidden = NO:

If View1.hidden = YES:

解决方案

Perhaps the easiest way to solve this issue is to manipulate constraint priorities.

I won't go into detail regarding how the Auto Layout system works – if you need that, check out WWDC sessions 202: Introduction to Auto Layout for iOS and OS X and 228: Best Practices for Mastering Auto Layout, both from 2012. Also, check out objc.io's great article on Auto Layout.

In short, constraints can be assigned priorities. A priority is represented by a floating point number in the range [0, 1000], inclusive. The special priority of 1000 means "required;" all other priorities are optional.


How can we use this to help? In Interface Builder (IB), create two different constraints for the top of your blue view. The first one should relate the top of the blue view to the bottom of the red view, with a distance of 0 points – that is, the top of the blue view should be flush with the bottom of the red view. We'll call this constraint the "unhidden" constraint.

The second constraint should go from the top of the blue view to the top of the superview, again with a distance of 0 points – that is, the top of the blue view should be flush with the top of its superview. We'll call this constraint the "hidden" constraint.

If you're following along, you've realized that it is impossible to satisfy both of these constraints simultaneously unless the red view's height is exactly equal to 0. That won't happen, though (I assume you're not resizing the red view, just hiding or unhiding it), so how can these constraints coexist? The answer lies in priorities. Make sure that, in IB, the priority of the unhidden constraint is 950 – remember, this optional, but at the high priority of 950. However, set the priority of the hidden constraint to something less than 950 – maybe 450, for example.

Next, you'll need to create outlets to these two constraints (how to do so is outside the scope of this answer – it's the same as creating outlets to anything in IB). I recommend naming them as I've named them here. So, in your header file, you might see the following:

...
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *unhiddenConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hiddenConstraint;
...

Now the only thing left is to alter the priorities of these constraints at the appropriate times – namely, when you hide or unhide the red view. So, in your view controller, add a method like this:

- (void)setRedViewHidden:(BOOL)hidden {
    if (hidden) {
        self.unhiddenConstraint.priority = 450;
        self.hiddenConstraint.priority   = 950;
        self.redView.hidden = YES;
    } else {
        self.hiddenConstraint.priority   = 450;
        self.unhiddenConstraint.priority = 950;
        self.redView.hidden = NO;
    }
}

Then, whenever you want to hide the red view, you just call this method with the proper argument. Setting the priorities on the constraints will implicitly mark the layout as dirty, causing a new layout pass to occur on the next turn of the UI loop.

This is not a beginner use of the Auto Layout engine. Let me know if you have any lingering questions about this solution.

这篇关于Xcode 6中的约束可以动态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:34