自动布局去除中间的看法时,倒塌的空间 [英] Autolayout collapsing space when removing intermediate views

查看:94
本文介绍了自动布局去除中间的看法时,倒塌的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点难倒就如何正确定义时给定的视图被删除,这将崩溃的观点之间的空间限制。我曾尝试添加纵向约束与大于或等于的关系,但是,它似乎最小距离满足约束不是preferred。

例如,有三个约束式给出V以下布局:[A] -5- [B],V:[B] -5- [℃],和V:[A] - (> = 5) - [C]:

  [查看A]
    |
   5磅
    |
[查看B]
    |
   5磅
    |
[查看℃]

删除视图B后,我希望它看起来是这样的:

  [查看A]
    |
   5磅
    |
[查看℃]

但它看起来是这样的:

  [查看A]
    |
   5 PT + 5的PT +视图B的高度
    |
[查看℃]


解决方案

如果你正在寻找这是可扩展的,你很可能将不得不做,在code。我使用类似

  UIView的*的SuperView = / *无论你的看法是上海华,可能在很多情况下self.view * /
NSArray的* arrViews = [NSArray的arrayWithObjects:/ *只放你想在这里显示,不把隐秘的事,并把它们以从上到下的事* /零]
CGFloat的缓冲= 5;[上海华addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews objectAtIndex:0]属性:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:上海华属性:NSLayoutAttributeTop倍数:1常数缓冲]];
的for(int i = 1; I< arrViews.count;我++)
{
     [上海华addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews objectAtIndex:我]属性:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:[arrViews objectAtIndex:I-1]属性:NSLayoutAttributeBottom倍数:1常数缓冲]];
}
[上海华addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews lastObject]属性:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:上海华属性:NSLayoutAttributeBottom倍数:1不变:-1 *(缓冲)];

这将会把一个固定的间距(尺寸= 缓存)前项之间,它是上海华,那么每个子视图和子视图之间的直接上面,那么之间底视图和上海华。你每次都这样叫你从 arrViews 删除项目,然后调用 [上海华needsLayout] 。你还需要确保高度约束莫名其妙定置,否则你会得到错误。如果一切将是相同的高度,你可以只添加其他行的循环添加一个高度约束为好。

I am a bit stumped as to how to properly define constraints that will collapse the space between views when a given view is removed. I have tried adding vertical constraints with a greater than or equal relation, however, it seems that the min distance to satisfy the constraint is not preferred.

For example, given the following layout with three constraints V:[A]-5-[B], V:[B]-5-[C], and V:[A]-(>=5)-[C]:

[ View A ]
    |
   5 pt
    |
[ View B ]
    |
   5 pt
    |
[ View C ]

After removing view B I want it to look like this:

[ View A ]
    |
   5 pt
    |
[ View C ]

But it looks like this:

[ View A ]
    |
   5 pt + 5 pt + height of view B
    |
[ View C ]

解决方案

If you're looking for this to be scalable, you're probably going to have to do it in code. I use something like

UIView *superView = /* whatever the superview of your views is, probably self.view in a lot of cases */
NSArray *arrViews = [NSArray arrayWithObjects:/* put only the things you want to show in here, do not put the hidden things, and put them in order from top to bottom */, nil];
CGFloat buffer = 5;

[superView addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews objectAtIndex:0] attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1 constant:buffer]];
for (int i = 1; i < arrViews.count; i++)
{
     [superView addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews objectAtIndex:i] attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:[arrViews objectAtIndex:i-1] attribute:NSLayoutAttributeBottom multiplier:1 constant:buffer]];
}
[superView addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews lastObject] attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1 constant:-1*(buffer)]]; 

This will put a fixed spacing (of size = buffer) between the top item and it's superview, then between each subview and the subview directly above it, then between the bottom view and the superview. You'll have to call this every time you remove an item from arrViews, then call [superView needsLayout]. You also need to make sure height constraints are set somehow, or you'll get errors. If everything is going to be the same height, you could just add another line in the loop to add a height constraint as well.

这篇关于自动布局去除中间的看法时,倒塌的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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