[英]Space evenly across a view without margins
I use following function to space a dynamic number of views across a View (with autolayout): 我使用以下函数在一个视图(使用自动布局)中动态分布多个视图:
-(NSArray*)spaceViews:(NSArray *)views onAxis:(UILayoutConstraintAxis)axis
{
NSAssert([views count] > 1,@"Can only distribute 2 or more views");
NSLayoutAttribute attributeForView;
NSLayoutAttribute attributeToPin;
switch (axis) {
case UILayoutConstraintAxisHorizontal:
attributeForView = NSLayoutAttributeCenterX;
attributeToPin = NSLayoutAttributeRight;
break;
case UILayoutConstraintAxisVertical:
attributeForView = NSLayoutAttributeCenterY;
attributeToPin = NSLayoutAttributeBottom;
break;
default:
return @[];
}
CGFloat fractionPerView = 1.0 / (CGFloat)([views count] + 1);
NSMutableArray *constraints = [NSMutableArray array];
[views enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop)
{
CGFloat multiplier = fractionPerView * (idx + 1.0);
[constraints addObject:[NSLayoutConstraint constraintWithItem:view
attribute:attributeForView
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:attributeToPin
multiplier:multiplier
constant:0.0]];
}];
[self addConstraints:constraints];
return [constraints copy];
}
(The function is from UIView-Autolayout ) (该功能来自UIView-Autolayout )
The problem is that it has margins. 问题在于它有边距。 How could this method be changed in order to evenly space the views without margins?
如何更改此方法以均匀分布视图而没有边距?
UPDATE UPDATE
It seems that the method is not working correctly after all, the margins on the outer items are much higher than at the rest. 看来该方法毕竟无法正常工作,外部项目的边距远高于其余部分。
As an example I changed the code for the left arm to: 例如,我将左臂的代码更改为:
self.leftArm = [UIView autoLayoutView];
self.leftArm.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.leftArm];
[self.leftArm pinAttribute:NSLayoutAttributeRight toAttribute:NSLayoutAttributeLeft ofItem:self.body withConstant:-10.0];
[self.leftArm pinAttribute:NSLayoutAttributeTop toSameAttributeOfItem:self.body withConstant:10.0];
[self.leftArm constrainToSize:CGSizeMake(20.0, 200.0)];
Now the robot looks like this: 现在,机器人看起来像这样:
Note that at the left arm the views are NOT evenly distributed, the margin on top and at the bottom are much higher. 请注意,左臂上的视图不是均匀分布的,顶部和底部的边距要高得多。 I can reproduce the same behavior horizontally and with less items.
我可以水平地复制相同的行为,并且可以减少项目。
I have successfully removed the margins by aligning the outer views with the sides of the superview and aligning the rest within the space between. 通过将外部视图与Superview的侧面对齐并将其余视图与它们之间的空间对齐,我已成功删除了页边空白。 But now, due to this problem, I always have bigger spaces between the outer views and the views next to them as between the rest.
但是现在,由于这个问题,我总是在外部视图和相邻视图之间以及其余视图之间留有更大的空间。
Does anyone know how to correct this? 有谁知道如何纠正这个问题?
UPDATE 2 更新2
I have created a fork and added my function to it, I also extended the robot example so you can see what I mean. 我创建了一个fork并添加了功能,我还扩展了机器人示例,以便您了解我的意思。
Please take a look . 请看看 。
It looks as though this code is from UIView-Autolayout . 看起来这段代码来自UIView-Autolayout 。 The latest version of the library has a new method which lets you stop the spacing from being added to edges:
该库的最新版本具有一个新方法,可让您停止将间距添加到边缘:
-(NSArray*)spaceViews:(NSArray*)views
onAxis:(UILayoutConstraintAxis)axis
withSpacing:(CGFloat)spacing
alignmentOptions:(NSLayoutFormatOptions)options
flexibleFirstItem:(BOOL)flexibleFirstItem
applySpacingToEdges:(BOOL)spaceEdges;
Just call this method instead and pass NO
to the last argument. 只需调用此方法,然后将
NO
传递给最后一个参数即可。
I have extended jrturton's UIView-Autolayout (based on this answer ) and created a pull request . 我扩展了jrturton的UIView-Autolayout (基于此答案 )并创建了pull请求 。
It is now possible to distribute the views evenly without margins by calling the new function: 现在可以通过调用新函数来均匀分布视图而没有空白:
-(NSArray*)spaceViews:(NSArray *)views
onAxis:(UILayoutConstraintAxis)axis
withMargin:(BOOL)margin
and setting the margin parameter to NO
. 并将margin参数设置为
NO
。
You can already find that version of UIView-Autolayout here . 您已经可以在此处找到该版本的UIView-Autolayout。
The brand new robot with evenly spaced views as teeth: 全新的机器人具有均匀分布的牙齿视图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.