如何为这些 UIButtons 设置边距
Posted
技术标签:
【中文标题】如何为这些 UIButtons 设置边距【英文标题】:How to set margin to these UIButtons 【发布时间】:2015-03-03 13:36:33 【问题描述】:我在Xcode中创建了一个有3个按钮的视图,代码如下:
UIImage *firstButtonImg = [UIImage imageNamed:(@"firstButton")];
UIImage *firstButtonImgHighlighted = [UIImage imageNamed:(@"firstButtonHightlighted")];
self.firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[firstButton setBackgroundImage:firstButtonImg forState:UIControlStateNormal];
[firstButton setBackgroundImage:firstButtonImgHighlighted forState:UIControlStateHighlighted];
self.firstButton.frame = CGRectMake(50, 60+self.aboutViewTop.frame.size.height+20, firstButtonImg.size.width, firstButtonImg.size.height);
[firstButton addTarget:self action:@selector(firstButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:firstButton];
UIImage *secondButtonImg = [UIImage imageNamed:(@"secondButton")];
UIImage *secondButtonImgHighlighted = [UIImage imageNamed:(@"secondButtonHighlighted")];
self.secondButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[secondButton setBackgroundImage:secondButtonImg forState:UIControlStateNormal];
[secondButton setBackgroundImage:secondButtonImgHighlighted forState:UIControlStateHighlighted];
self.secondButton.frame = CGRectMake(50, 60+aboutViewTop.frame.size.height+firstButton.frame.size.height+20.f, secondButtonImg.size.width, secondButtonImg.size.height);
[secondButton addTarget:self action:@selector(secondButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:secondButton];
UIImage *thirdButtonImg = [UIImage imageNamed:(@"thirdButton")];
UIImage *thirdButtonImgHighlighted = [UIImage imageNamed:(@"thirdButtonHighlighted")];
self.thirdButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[thirdButton setBackgroundImage:thirdButtonImg forState:UIControlStateNormal];
[thirdButton setBackgroundImage:thirdButtonImgHighlighted forState:UIControlStateHighlighted];
self.thirdButton.frame = CGRectMake(50, 60+aboutViewTop.frame.size.height+firstButton.frame.size.height+secondButton.frame.size.height+20.f, thirdButtonImg.size.width, thirdButtonImg.size.height);
[thirdButton addTarget:self action:@selector(thirdButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:thirdButton];
但是这些按钮位置很近,没有我在它们之间添加的 20 个空格,我不知道为什么
请帮帮我
【问题讨论】:
请问问自己,您是否真的需要在代码中进行 UI 工作,而不是使用 Interface Builder。使用自动布局将节省您尝试解决此类问题的时间。 【参考方案1】:对不起各位
问题解决了
我好像不能多次添加数字到rect
正确的代码:
UIImage *firstButtonImg = [UIImage imageNamed:(@"firstButton")];
UIImage *firstButtonImgHighlighted = [UIImage imageNamed:(@"firstButtonHightlighted")];
self.firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[firstButton setBackgroundImage:firstButtonImg forState:UIControlStateNormal];
[firstButton setBackgroundImage:firstButtonImgHighlighted forState:UIControlStateHighlighted];
self.firstButton.frame = CGRectMake(50, 80+self.aboutViewTop.frame.size.height, firstButtonImg.size.width, firstButtonImg.size.height);
[firstButton addTarget:self action:@selector(firstButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:firstButton];
UIImage *secondButtonImg = [UIImage imageNamed:(@"secondButton")];
UIImage *secondButtonImgHighlighted = [UIImage imageNamed:(@"secondButtonHighlighted")];
self.secondButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[secondButton setBackgroundImage:secondButtonImg forState:UIControlStateNormal];
[secondButton setBackgroundImage:secondButtonImgHighlighted forState:UIControlStateHighlighted];
self.secondButton.frame = CGRectMake(50, 100+aboutViewTop.frame.size.height+firstButton.frame.size.height, secondButtonImg.size.width, secondButtonImg.size.height);
[secondButton addTarget:self action:@selector(secondButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:secondButton];
UIImage *thirdButtonImg = [UIImage imageNamed:(@"thirdButton")];
UIImage *thirdButtonImgHighlighted = [UIImage imageNamed:(@"thirdButtonHighlighted")];
self.thirdButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[thirdButton setBackgroundImage:thirdButtonImg forState:UIControlStateNormal];
[thirdButton setBackgroundImage:thirdButtonImgHighlighted forState:UIControlStateHighlighted];
self.thirdButton.frame = CGRectMake(50, 120+aboutViewTop.frame.size.height+firstButton.frame.size.height+secondButton.frame.size.height, thirdButtonImg.size.width, thirdButtonImg.size.height);
[thirdButton addTarget:self action:@selector(thirdButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:thirdButton];
【讨论】:
【参考方案2】:试试这个
float yValue = 60+self.aboutViewTop.frame.size.height+20;
self.firstButton.frame = CGRectMake(50, yValue, firstButtonImg.size.width, firstButtonImg.size.height);
yValue = CGRectGetMaxY(self.firstButton.frame)+20; //y+height of first button+20 spacing
self.secondButton.frame = CGRectMake(50, yValue, secondButtonImg.size.width, secondButtonImg.size.height);
yValue = CGRectGetMaxY(self.secondButton.frame)+20;
self.thirdButton.frame = CGRectMake(50, yValue, thirdButtonImg.size.width, thirdButtonImg.size.height);
【讨论】:
【参考方案3】:self.firstButton.frame = CGRectMake(50, 60+self.aboutViewTop.frame.size.height+20, firstButtonImg.size.width, firstButtonImg.size.height);
第一种情况:60+20+topViewHeight
self.secondButton.frame = CGRectMake(50, 60+aboutViewTop.frame.size.height+firstButton.frame.size.height+20.f, secondButtonImg.size.width, secondButtonImg.size.height);
你得到: 60+20+topViewHeight+firstButtonHeight
所以没有边距。 我认为您必须先将 20 添加到第二个按钮框架:
self.secondButton.frame = CGRectMake(50, 60+aboutViewTop.frame.size.height+**20.f**+firstButton.frame.size.height+20.f, secondButtonImg.size.width, secondButtonImg.size.height);
40 到第三:
self.thirdButton.frame = CGRectMake(50, 60+aboutViewTop.frame.size.height+firstButton.frame.size.height+**40.f**+secondButton.frame.size.height+20.f, thirdButtonImg.size.width, thirdButtonImg.size.height);
【讨论】:
以上是关于如何为这些 UIButtons 设置边距的主要内容,如果未能解决你的问题,请参考以下文章
目标c如何为整个应用程序上的所有UIbuttons设置独家触摸