2015-07-19 28 views
0

我想根据数组中的按钮数来中心按钮。基于阵列中n个图像的视图中的中心图像

因此,与一个对象,你会通过以下居中:

CGRect originalFrame = self.view.frame; 
button.frame = CGRectMake(orignalFrame.size.width/2 - self.button.frame.size.width/2, etc, etc, etc); 

然而,当我在一个数组对象的n个,我有一个很难中心他们。以下是我有这么远,但它从来没有对齐X:

-(void)displayIconsFromSignedInArray:(NSArray *)array { 
    CGFloat buttonWidthHeight = 50; 

    CGFloat x = (self.view.frame.size.width/array.count) - (buttonWidthHeight/2)*array.count; 

    for (UIButton *button in array) { 
    button.frame = CGRectMake(x, self.view.frame.size.height/2 - buttonWidthHeight/2, buttonWidthHeight, buttonWidthHeight); 
    x += buttonWidthHeight + 2.5; 
    [self.view addSubview:button]; 
    } 
} 

而这正是我结束了:

image1

从外观上来看FB图标的起源是在宽度的一半处开始,但我无法得到正确的结果。请帮助我了解执行此操作的过程,无论是更好的方式还是以这种方式进行修复。谢谢。

回答

1

第一偏移量=半宽度 - 所有按钮宽度的一半。也许你需要考虑视图无法显示连续按钮的情况。

CGFloat x = self.view.frame.size.width/2 - buttonWidthHeight*array.count/2; 
+0

很好地完成。谢谢 –