在视图控制器的宽度上水平均匀分布UIButton的最简单的方法是什么?

我已经看了很多答案,他们都似乎很复杂! 最近我在看这个答案,但我不想把我的button放在视图内。

我有6个UIButtons都是相同的尺寸。 我想要将它们在整个宽度和我的根视图控制器的底部水平均匀地分开。

| | | | | [b1] [b2] [b3] [b4] [b5] [b6] | ________________________________________ 

什么是以编程方式实现这个最简单的方法?

我认为这比接受的答案更简单。 好的,首先让我们创build一些button:

 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem]; button1.translatesAutoresizingMaskIntoConstraints = NO; [button1 setTitle:@"Btn1" forState:UIControlStateNormal]; 

…为6个button做6次,然而你喜欢然后把它们添加到视图中:

 [self.view addSubview:button1]; [self.view addSubview:button2]; [self.view addSubview:button3]; [self.view addSubview:button4]; [self.view addSubview:button5]; [self.view addSubview:button6]; 

修复一个button到视图的底部:

 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; 

然后告诉所有的button是相等的宽度,并在整个宽度上均匀分布:

 NSDictionary *views = NSDictionaryOfVariableBindings(button1, button2, button3, button4, button5, button6); [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button1][button2(==button1)][button3(==button1)][button4(==button1)][button5(==button1)][button6(==button1)]|" options:NSLayoutFormatAlignAllBottom metrics:nil views:views]]; 

结果:

在这里输入图像说明

我知道这个问题有点老,但我已经在iOS上编程了几年,不喜欢使用自动布局。 所以我写了一个辅助方法来均匀地将UIButtons水平放置,并将它们垂直放置在一个UIView中。 这适用于菜单栏。

 - (void) evenlySpaceTheseButtonsInThisView : (NSArray *) buttonArray : (UIView *) thisView { int widthOfAllButtons = 0; for (int i = 0; i < buttonArray.count; i++) { UIButton *thisButton = [buttonArray objectAtIndex:i]; [thisButton setCenter:CGPointMake(0, thisView.frame.size.height / 2.0)]; widthOfAllButtons = widthOfAllButtons + thisButton.frame.size.width; } int spaceBetweenButtons = (thisView.frame.size.width - widthOfAllButtons) / (buttonArray.count + 1); UIButton *lastButton = nil; for (int i = 0; i < buttonArray.count; i++) { UIButton *thisButton = [buttonArray objectAtIndex:i]; if (lastButton == nil) { [thisButton setFrame:CGRectMake(spaceBetweenButtons, thisButton.frame.origin.y, thisButton.frame.size.width, thisButton.frame.size.height)]; } else { [thisButton setFrame:CGRectMake(spaceBetweenButtons + lastButton.frame.origin.x + lastButton.frame.size.width, thisButton.frame.origin.y, thisButton.frame.size.width, thisButton.frame.size.height)]; } lastButton = thisButton; } } 

只需将此方法复制并粘贴到任何视图控制器中。 然后访问它,我首先创build了我想要的所有button,然后用数组中的所有button以及我想要的UIView调用该方法。

 [self evenlySpaceTheseButtonsInThisView:@[menuButton, hierarchyMenuButton, downButton, upButton] :menuView]; 

这种方法的优点是你不需要自动布局,实现起来非常简单。 缺点是,如果你的应用程序在横向和纵向工作,你将需要确保在视图旋转后再次调用这个方法。

我通常做类似的事情:

 int numButtons = 6; float gap = 10.0f; float y = 50.0f; float width = (self.view.frame.size.width - gap * (numButtons + 1)) / numButtons; float height = 60.0f; for (int n=0;n<numButtons;n++) { float x = gap * (n+1) + width * n; UIButton *button = [self.buttons objectAtIndex:n]; //Or get button some other way/make button. [button setFrame:CGRectMake(x,y,width,height)]; } 

您可以将numButtons设置为行中所需的多个button,并且如果您有一组button,则可以将其设置为该数组的length

y只是你想要的任何坐标,高度和间隙也是一样,这是button之间的空间。 宽度只是根据每个button之间的屏幕宽度和间隔空间计算每个button的宽度。

您可以将标签/button/视图的数组发送到此方法并排列button框架。

 -(void)arrangeViewsXposition:(NSArray*)anyView y:(CGFloat)y width:(CGFloat)width height:(CGFloat)height mainViewWdith:(UIView*)mainview { int count = (int)anyView.count; CGFloat widthTemp = mainview.bounds.size.width, temp1 = widthTemp-(width*count),space = temp1/(count+1); for (int i = 0; i<count; i++) { UIView *btnTemp = (UIView*)[anyView objectAtIndex:i]; if (btnTemp) { btnTemp.frame = CGRectMake(space+((space+width)*i), y, width, height); } } } 

像这样调用这个方法:

 [self arrangeViewsXposition:@[btnSave,btnCancel] y:5 width:80 height:30 mainViewWdith:footerView]; 

在这个问题中讨论了一些体面的自动布局解决scheme:

在容器视图中均匀地分隔多个视图

基本上它可以是很多手动的约束定义代码,它们可以方便地包装在一个类别扩展中用于封装/重用。

我只是使用制图在Swift中制作而成。

 func disributeEvenlyAcrossWithCartography(views: [UIView], enclosingBox: UIView, spacer: CGFloat = 10.0) { var priorView = UIView() // never null for (index, view) in views.enumerate() { constrain(view, priorView, enclosingBox) { view, prior, enclosingBox in view.height == enclosingBox.height view.centerY == enclosingBox.centerY if index == 0 { view.width == enclosingBox.width / CGFloat(views.count) - spacer view.left == enclosingBox.left } else { view.left == prior.right + (spacer + spacer / CGFloat(views.count - 1)) view.width == prior.width } } priorView = view } } 

结果与5个意见和一个小的间隔:

在这里输入图像说明

这是我在Stackoverflow上的第一篇文章。 这个网站已经非常有助于让我加快Xcode和迅速。 无论如何,下面是我对上述问题的快速修复。

分别固定最左边的button和最右边的button。 在每个button之间创build“虚拟”标签。 将所有“虚拟”标签设置为相等的宽度。 对于每个“虚拟”标签,添加约束条件(0到最左边的邻居,0到最右边的邻居)。 即使从纵向转换到横向,button也将始终等距离分布。