ios - 水平均匀分布三个按钮ios

标签 ios objective-c uiview uibutton frame

我有三个UIButtons和一个支架UIView我想要我的三个 UIButtonUIView 内垂直居中并且我希望它们在水平方向上均匀分布

float ycentre = CGRectGetMidY(self.buttonHolderView.bounds);
float xposition = (self.buttonHolderView.frame.size.width - 135.0f)/4.0f;


self.button1 = [[UIButton alloc]initWithFrame:CGRectMake(xposition,0, 45, 30)];
self.button1.center = CGPointMake(self.button1.frame.origin.x, ycentre);


self.button2 = [[UIButton alloc]initWithFrame:CGRectMake((xposition*2)+45,0, 45, 30)];
self.button2.center = CGPointMake(self.button2.frame.origin.x, ycentre);

self.button3 = [[UIButton alloc]initWithFrame:CGRectMake((xposition*3)+90,0, 45, 30)];
self.button3.center = CGPointMake(self.button3.frame.origin.x, ycentre);
[self.button3 addTarget:self action:@selector(ShareButtonClicked) forControlEvents:UIControlEventTouchUpInside];

[self.buttonHolderView addSubview:self.button1];
[self.buttonHolderView addSubview:self.button2];
[self.buttonHolderView addSubview:self.button3];

每个按钮的高度为 30 和宽度为 45,但我的代码没有平均分配它们,这是怎么回事?

最佳答案

将它们放在 UIStackView 中。它使 View 之间的间隔非常容易。

关于ios - 水平均匀分布三个按钮ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44344503/

相关文章:

ios - 如何询问设备屏幕比例(iphone 或 ipad)?

ios - 计算器除以零

ios - 核心数据子查询 : no such column

objective-c - 考虑到仅将选定的日期计入工作日,找到最终日期的逻辑是什么?

ios - View over view 打破层次结构

iOS 无法为 FXBlurView 设置背景色

ios - Xcode 6、ios 8 模拟器无法启动

ios - UICollectionViewCell 复选标记返回 nil

objective-c - cocoa文件关联问题

ios - autoresizingMask什么时候实现?