[英]Center images in view based on n images in array
我正在尝试根据数组中按钮的数量将按钮居中。
因此,对于一个对象,可以通过以下方式将其居中:
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];
}
}
这就是我最终得到的结果:
从外观上看,FB图标的原点始于宽度的一半,但我无法正确理解。 请帮助我了解执行此操作的过程,这是一种更好的方法,或者是通过这种方法来解决的。 谢谢。
第一个偏移量=一半宽度-所有按钮宽度的一半。 也许您需要考虑视图无法连续显示按钮的情况。
CGFloat x = self.view.frame.size.width/2 - buttonWidthHeight*array.count/2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.