Ios 水平均匀分布三个按钮
我有三个Ios 水平均匀分布三个按钮,ios,objective-c,uiview,uibutton,frame,Ios,Objective C,Uiview,Uibutton,Frame,我有三个ui按钮和一个支架ui视图我希望我的三个ui按钮在ui视图内垂直居中,并且我希望它们在水平方向上均匀分布 float ycentre = CGRectGetMidY(self.buttonHolderView.bounds); float xposition = (self.buttonHolderView.frame.size.width - 135.0f)/4.0f; self.button1 = [[UIButton alloc]initWithFrame:CGRectMake
ui按钮
和一个支架ui视图
我希望我的三个ui按钮
在ui视图
内垂直居中,并且我希望它们在水平方向上均匀分布
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
中。它使视图之间的间隔变得非常均匀。将它们放置在UIStackView
中。它使视图间隔均匀,非常容易。按如下所示设置按钮的框
,不要设置中心
float thirdOfWidth = (self.buttonHolderView.frame.width/3.0);
float buttonWidth = 45.0;
float buttonHeight = 30.0;
self.button1 = [[UIButton alloc]initWithFrame:CGRectMake(thirdOfWidth*0 + (thirdOfWidth - buttonWidth)/2, (self.buttonHolderView.frame.height - buttonHeight)/2, 45, 30)];
self.button2 = [[UIButton alloc]initWithFrame:CGRectMake(thirdOfWidth*1 + (thirdOfWidth - buttonWidth)/2, (self.buttonHolderView.frame.height - buttonHeight)/2, 45, 30)];
self.button3 = [[UIButton alloc]initWithFrame:CGRectMake(thirdOfWidth*2 + (thirdOfWidth - buttonWidth)/2, (self.buttonHolderView.frame.height - buttonHeight)/2, 45, 30)];
按如下方式设置按钮的
框架
,不要设置中心
float thirdOfWidth = (self.buttonHolderView.frame.width/3.0);
float buttonWidth = 45.0;
float buttonHeight = 30.0;
self.button1 = [[UIButton alloc]initWithFrame:CGRectMake(thirdOfWidth*0 + (thirdOfWidth - buttonWidth)/2, (self.buttonHolderView.frame.height - buttonHeight)/2, 45, 30)];
self.button2 = [[UIButton alloc]initWithFrame:CGRectMake(thirdOfWidth*1 + (thirdOfWidth - buttonWidth)/2, (self.buttonHolderView.frame.height - buttonHeight)/2, 45, 30)];
self.button3 = [[UIButton alloc]initWithFrame:CGRectMake(thirdOfWidth*2 + (thirdOfWidth - buttonWidth)/2, (self.buttonHolderView.frame.height - buttonHeight)/2, 45, 30)];
它也可以通过任意数量的按钮在循环中完成
NSInteger count = 3;
CGFloat buttonWidth = 45;
CGFloat buttonHeight = 30;
CGFloat x = (self.bounds.size.width - buttonWidth) / 2;
CGFloat verticalSpacing = ((self.bounds.size.height - buttonHeight * count) / count) - buttonHeight / 2;
CGFloat y = verticalSpacing;
for (int i = 0; i < count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, buttonWidth, buttonHeight)];
[self addSubview:button];
y += buttonHeight + verticalSpacing;
}
NSInteger计数=3;
CGFloat按钮宽度=45;
CGFloat按钮高度=30;
CGFloat x=(self.bounds.size.width-buttonWidth)/2;
CGFloat垂直间距=((self.bounds.size.height-buttonHeight*count)/count)-buttonHeight/2;
cGy=垂直间距;
for(int i=0;i
也可以通过任意数量的按钮在循环中完成
NSInteger count = 3;
CGFloat buttonWidth = 45;
CGFloat buttonHeight = 30;
CGFloat x = (self.bounds.size.width - buttonWidth) / 2;
CGFloat verticalSpacing = ((self.bounds.size.height - buttonHeight * count) / count) - buttonHeight / 2;
CGFloat y = verticalSpacing;
for (int i = 0; i < count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, buttonWidth, buttonHeight)];
[self addSubview:button];
y += buttonHeight + verticalSpacing;
}
NSInteger计数=3;
CGFloat按钮宽度=45;
CGFloat按钮高度=30;
CGFloat x=(self.bounds.size.width-buttonWidth)/2;
CGFloat垂直间距=((self.bounds.size.height-buttonHeight*count)/count)-buttonHeight/2;
cGy=垂直间距;
for(int i=0;i
post UI您正在寻找的内容…post UI您正在寻找的内容…最佳方法。欢呼UIStackView
…:PNoteUIStackView
需要iOS 9.0+的最佳方法。欢呼UIStackView
…:PNoteUIStackView
需要iOS 9.0+