我有一个奇怪的问题:
我在ViewDidLoad或ViewDidAppear中动态设置一些按钮位置(尝试了两者)并手动设置尺寸。然后,只要按下按钮,我就会改变按钮的框架。但即使我设置了原始版本 - 它们在屏幕上的位置不同,但它们看起来有点像“向下移动了一些px”。我附上代码和sceenshot:
viewDidAppear:
self.twoTeamsBtn = [[UIButton alloc] init];
self.twoTeamsBtn.frame = CGRectMake(156/2, (94/2), selected2Image.size.width, selected2Image.size.height);
self.threeTeamsBtn = [[UIButton alloc] init];
self.threeTeamsBtn.frame = CGRectMake(331/2, (132/2), unselected3Image.size.width, unselected3Image.size.height);
self.fourTeamsBtn = [[UIButton alloc] init];
self.fourTeamsBtn.frame = CGRectMake(505/2, (132/2), unselected4Image.size.width, unselected4Image.size.height);
所以,两队是94/2(Y),而你是132/2。两个图像都具有相同的高度。 (selected2与selected3相同,unselected3与unselected3相同,依此类推。)
然后在我的点击方法中,我有:
if (button.tag == self.twoTeamsBtn.tag) {
self.threeTeamsBtn.frame = CGRectMake(331/2, (132/2), self.threeTeamsBtn.frame.size.width, self.threeTeamsBtn.frame.size.height);
self.fourTeamsBtn.frame = CGRectMake(505/2, (132/2), self.fourTeamsBtn.frame.size.width, self.fourTeamsBtn.frame.size.height);
self.twoTeamsBtn.frame = CGRectMake(156/2, (94/2), selected2Image.size.width, selected2Image.size.height);
[self.threeTeamsBtn setImage:unselected3Image forState:UIControlStateNormal];
[self.twoTeamsBtn setImage:selected2Image forState:UIControlStateNormal];
[self.fourTeamsBtn setImage:unselected4Image forState:UIControlStateNormal];
} else if (button.tag == self.threeTeamsBtn.tag) {
self.threeTeamsBtn.frame = CGRectMake(331/2, (94/2), selected3Image.size.width, selected3Image.size.height);
self.fourTeamsBtn.frame = CGRectMake(505/2, (132/2), self.fourTeamsBtn.frame.size.width, self.fourTeamsBtn.frame.size.height);
self.twoTeamsBtn.frame = CGRectMake(156/2, (132/2), self.twoTeamsBtn.frame.size.width, self.twoTeamsBtn.frame.size.height);
[self.threeTeamsBtn setImage:selected3Image forState:UIControlStateNormal];
[self.twoTeamsBtn setImage:unselected2Image forState:UIControlStateNormal];
[self.fourTeamsBtn setImage:unselected4Image forState:UIControlStateNormal];
} else if (button.tag == self.fourTeamsBtn.tag) {
self.threeTeamsBtn.frame = CGRectMake(331/2, (132/2), self.threeTeamsBtn.frame.size.width, self.threeTeamsBtn.frame.size.height);
self.fourTeamsBtn.frame = CGRectMake(505/2, (94/2), selected4Image.size.width, selected4Image.size.height);
self.twoTeamsBtn.frame = CGRectMake(156/2, (132/2), self.twoTeamsBtn.frame.size.width, self.twoTeamsBtn.frame.size.height);
[self.threeTeamsBtn setImage:unselected3Image forState:UIControlStateNormal];
[self.twoTeamsBtn setImage:unselected2Image forState:UIControlStateNormal];
[self.fourTeamsBtn setImage:selected4Image forState:UIControlStateNormal];
}
屏幕截图如下:
因此,如果您在第二张图片中看到按钮2和按钮4不在同一级别,尽管它们的帧相同。在第3个中,2和3处于同一水平。
就像原来的132/2一样,与以后的设置不一样。
我错过了什么吗?
答案 0 :(得分:2)
我很感谢您可能已经找到了答案感谢TwoStraws,但我认为问题取决于您设置框架宽度/高度的方式
if (button.tag == self.twoTeamsBtn.tag) {
self.threeTeamsBtn.frame = CGRectMake(331/2, (132/2), self.threeTeamsBtn.frame.size.width, self.threeTeamsBtn.frame.size.height);
self.fourTeamsBtn.frame = CGRectMake(505/2, (132/2), self.fourTeamsBtn.frame.size.width, self.fourTeamsBtn.frame.size.height);
self.twoTeamsBtn.frame = CGRectMake(156/2, (94/2), selected2Image.size.width, selected2Image.size.height);
[self.threeTeamsBtn setImage:unselected3Image forState:UIControlStateNormal];
[self.twoTeamsBtn setImage:selected2Image forState:UIControlStateNormal];
[self.fourTeamsBtn setImage:unselected4Image forState:UIControlStateNormal];
}
对于未选择的图像,您将帧设置为之前的任何值,但如果选定/未选择的图像大小不同,则选择的最后一个(现在未选择的图像将关闭)。您需要根据正在使用的图像设置帧大小。
即
if (button.tag == self.twoTeamsBtn.tag) {
self.threeTeamsBtn.frame = CGRectMake(331/2, (132/2), unselected3Image.size.width, unselected3Image.size.height);
self.fourTeamsBtn.frame = CGRectMake(505/2, (132/2), unselected4Image.size.width, unselected4Image.size.height);
self.twoTeamsBtn.frame = CGRectMake(156/2, (94/2), selected2Image.size.width, selected2Image.size.height);
[self.threeTeamsBtn setImage:unselected3Image forState:UIControlStateNormal];
[self.twoTeamsBtn setImage:selected2Image forState:UIControlStateNormal];
[self.fourTeamsBtn setImage:unselected4Image forState:UIControlStateNormal];
}
答案 1 :(得分:1)
Changing frames of things is best avoided if you're able to – it's preferable to adjust thebounds
and center
instead, which in turn recalculates the frame
for you.
In your particular example, animating the transform
property is even easier, using something like this:
// animate upwards
btn.transform = CGAffineTransformMakeTranslation(0, -50)
// go back to where it was
btn.transform = CGAffineTransformIdentity
The CGAffineTransformIdentity
transform automatically puts the button back where it was, which is what makes this solution so easy.
If you are also changing the image of the button, the best thing to do if you can is make sure your before and after pictures are the same size, perhaps by adding a small amount of whitespace to the smaller of the two. This way your buttons never change in size, which means you have one less thing to worry about.
答案 2 :(得分:-1)
btn1 = [[UIButton alloc] init];
btn1.frame = CGRectMake(156/2, (94/2), 50, 50);
btn2 = [[UIButton alloc] init];
btn2.frame = CGRectMake(331/2, (132/2), 50, 50);
btn3 = [[UIButton alloc] init];
btn3.frame = CGRectMake(505/2, (132/2), 50, 50);
[btn1 setBackgroundColor:[UIColor blackColor]];
[btn2 setBackgroundColor:[UIColor redColor]];
[btn3 setBackgroundColor:[UIColor blueColor]];
[btn1 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[btn3 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
btn1.tag=1000;
btn2.tag=2000;
btn3.tag=3000;
[self.view addSubview:btn1];
[self.view addSubview:btn2];
[self.view addSubview:btn3];
将此代码放在ViewDidAppear或ViewDidLoad中。它会起作用