我在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个空格,我无法弄清楚为什么
请帮帮我
答案 0 :(得分:0)
对不起家伙
问题解决了
似乎我无法多次向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];
答案 1 :(得分:0)
试试这个
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);
答案 2 :(得分:0)
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);