iOS的自动布局:等于空间以适合Superviews宽度 [英] iOS Auto Layout: Equal Spaces to Fit Superviews Width
问题描述
可能重复:结果
自动布局均匀间距
块引用>我试图创建带有按钮的滚动条(类似于
UISegmentedControl
)。在上海华是的UIScrollView
。只要按钮不适合滚动视图,滚动视图应该是滚动的。到目前为止,几乎一切正常:随着大量按钮(滚动到右):
没有那么多按钮:
现在,我的目标是,如果有空间的所有按钮,就应该在整个320像素观点同样US $ p $垫。 如何可以我定义的空间约束的按钮之间?
现在,我使用下面的约束(self是一个
的UIScrollView
)UIView的* firstButton = self.buttons [0];[self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@| - (5) - [firstButton]选项:0的指标:无意见:NSDictionaryOfVariableBindings(firstButton)];UIView的* lastButton = [self.buttons lastObject][self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@[lastButton] - (5) - |选项:0的指标:无意见:NSDictionaryOfVariableBindings(lastButton)];UIView的* previousView =零;对于(的UIView *在self.buttons视图){
如果(previousView){
[self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@[previousView] - (5) - [观点]选项:0的指标:无意见:NSDictionaryOfVariableBindings(previousView,视图)];
}
previousView =视图;
}如果我改变了上海华的类型从
的UIScrollView
到的UIView
,我得到以下的结果,仍不是我想要的,但至少它看起来在过去的按钮,它关系到右侧边缘的约束(是有道理的,这不会发生的滚动视图,作为内容的大小自动设置):
任何想法?
解决方案- (无效){viewWillLayoutSubviews
// *的UIButton按钮1,*按钮2,*按钮3,*键4;
// NSMutableArray里* constraintsForButtons; 浮unusedHorizontalSpace = self.view.bounds.size.width - button1.intrinsicContentSize.width - button2.intrinsicContentSize.width - button3.intrinsicContentSize.width - button4.intrinsicContentSize.width;
*的NSNumber = spaceBetweenEachButton [NSNumber的numberWithFloat:unusedHorizontalSpace / 5]; [self.view removeConstraints:constraintsForButtons];
[constraintsForButtons removeAllObjects]
[constraintsForButtons addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@H:| - (空间) - [按钮1] - (空间) - [按钮2] - (空间) - [按钮3] - (空间) - [将Button4] - (空间) - |
选项:NSLayoutFormatAlignAllCenterY
指标:@ {@空间:spaceBetweenEachButton}
观点:NSDictionaryOfVariableBindings(按钮1,按钮2,按钮3,将Button4)];
[constraintsForButtons addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@V:| [按钮1]
选项:0
指标:无
观点:NSDictionaryOfVariableBindings(按钮1)]];
[self.view addConstraints:constraintsForButtons];
}这是不是pretty是你的,并且它假定有4个按钮,但它同样空间他们。也就是说,按钮之间的空的空间都具有相同的宽度。这并不意味着,按钮1和按钮2的NSLayoutAttributeLeading之间的距离是相同的按钮2&放大器之间的距离;按钮3的。
Possible Duplicate:
Autolayout Even SpacingI'm trying to create a scrollable bar with buttons (similar to a
UISegmentedControl
). The superview is anUIScrollView
. As soon as the buttons don't fit into the scrollview, the scrollview should be scrollable. So far, almost everything works fine:With a lot of buttons (scrolled to the right):
Not so many buttons:
Now, my goal is that if there is room for all buttons, they should be equally spread across the whole 320px view. How can I define constrains for the spaces in between the buttons?
Right now, I'm using the following constraints (self is a
UIScrollView
):UIView *firstButton = self.buttons[0]; [self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(5)-[firstButton]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(firstButton)]]; UIView *lastButton = [self.buttons lastObject]; [self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"[lastButton]-(5)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lastButton)]]; UIView *previousView = nil; for (UIView *view in self.buttons) { if (previousView) { [self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"[previousView]-(5)-[view]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(previousView, view)]]; } previousView = view; }
If I change the type of the superview from
UIScrollView
to anUIView
, I get the following result, still not what I want, but at least it looks for the constraint of the last button that ties it to the right edge (makes sense, that this doesn't happen for the scrollview, as the content size is automatically set):Any ideas?
解决方案- (void) viewWillLayoutSubviews { // UIButton *button1, *button2, *button3, *button 4 ; // NSMutableArray *constraintsForButtons ; float unusedHorizontalSpace = self.view.bounds.size.width - button1.intrinsicContentSize.width - button2.intrinsicContentSize.width - button3.intrinsicContentSize.width - button4.intrinsicContentSize.width ; NSNumber* spaceBetweenEachButton= [NSNumber numberWithFloat: unusedHorizontalSpace / 5 ] ; [self.view removeConstraints:constraintsForButtons] ; [constraintsForButtons removeAllObjects] ; [constraintsForButtons addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|-(space)-[button1]-(space)-[button2]-(space)-[button3]-(space)-[button4]-(space)-|" options: NSLayoutFormatAlignAllCenterY metrics: @{@"space":spaceBetweenEachButton} views: NSDictionaryOfVariableBindings(button1,button2,button3,button4) ] ] ; [constraintsForButtons addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|[button1]" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(button1) ] ] ; [self.view addConstraints:constraintsForButtons] ; }
This isn't as pretty as yours, and it assumes there are 4 buttons, but it equally spaces them. That is, the empty spaces between the buttons all have the same width. This does not mean that the distance between the NSLayoutAttributeLeading of button1 and button2 is the same as the distance between button2 & button3's.
这篇关于iOS的自动布局:等于空间以适合Superviews宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!