Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 水平均匀分布三个按钮_Ios_Objective C_Uiview_Uibutton_Frame - Fatal编程技术网

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
…:PNote
UIStackView
需要iOS 9.0+的最佳方法。欢呼
UIStackView
…:PNote
UIStackView
需要iOS 9.0+