我有一个奇怪的问题:
我在 ViewDidLoad 或 ViewDidAppear 中动态设置一些按钮位置(都尝试过)并手动设置尺寸。然后,每当按下按钮时,我都会更改按钮的框架。但即使我设置了原始的 - 它们在屏幕上的位置不同,但它们看起来有点“向下移动了一些 px”。我附上代码和截图:
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);
所以,twoTeams 的赔率为 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 不在同一级别,尽管它们的框架是相同的。在第三个中,2 和 3 处于同一水平。
比如原来的132/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];
}
关于IOS - uibutton的动态放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34369917/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |