菜鸟教程小白 发表于 2022-12-12 17:54:54

IOS - uibutton的动态放置


                                            <p><p>我有一个奇怪的问题:</p>

<p>我在 ViewDidLoad 或 ViewDidAppear 中动态设置一些按钮位置(都尝试过)并手动设置尺寸。然后,每当按下按钮时,我都会更改按钮的框架。但即使我设置了原始的 - 它们在屏幕上的位置不同,但它们看起来有点“向下移动了一些 px”。我附上代码和截图:</p>

<p>viewDidAppear :</p>

<pre><code>    self.twoTeamsBtn = [ init];
    self.twoTeamsBtn.frame = CGRectMake(156/2, (94/2), selected2Image.size.width, selected2Image.size.height);
    self.threeTeamsBtn = [ init];
    self.threeTeamsBtn.frame = CGRectMake(331/2, (132/2), unselected3Image.size.width, unselected3Image.size.height);
    self.fourTeamsBtn = [ init];
    self.fourTeamsBtn.frame = CGRectMake(505/2, (132/2), unselected4Image.size.width, unselected4Image.size.height);
</code></pre>

<p>所以,twoTeams 的赔率为 94/2 (Y),而你的赔率为 132/2。两个图像的高度相同。
(selected2和selected3一样,unselected3和unselected3一样,以此类推)。</p>

<p>然后在我的点击方法中我有:</p>

<pre><code> 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);

    ;
    ;
    ;

} 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);

    ;
    ;
    ;

} 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);

    ;
    ;
    ;

}
</code></pre>

<p>截图如下:</p>

<p> <a href="/image/Bx3tt.jpg" rel="noreferrer noopener nofollow"><img src="/image/Bx3tt.jpg" alt="button 2 selected"/></a>
<a href="/image/ZWgk9.jpg" rel="noreferrer noopener nofollow"><img src="/image/ZWgk9.jpg" alt="button 3 selected"/></a>
<a href="/image/p2HsG.jpg" rel="noreferrer noopener nofollow"><img src="/image/p2HsG.jpg" alt="button 3 selected"/></a> </p>

<p>因此,如果您在第二张图片中看到,按钮 2 和按钮 4 不在同一级别,尽管它们的框架是相同的。在第三个中,2 和 3 处于同一水平。</p>

<p><strong>比如原来的132/2,和后面的不一样。</strong></p>

<p>我错过了什么吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>感谢 TwoStraws,您可能已经找到了答案,但我认为问题在于您设置框架宽度/高度的方式</p>

<pre><code>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);

    ;
    ;
    ;
}
</code></pre>

<p>对于未选择的图像,您将框架设置为之前的任何图像,但如果选择/未选择的图像大小不同,则最后一个选择的图像(现在未选择的图像将关闭)。您需要根据您使用的图像设置帧大小。</p>

<p>即</p>

<pre><code>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);

    ;
    ;
    ;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于IOS - uibutton的动态放置,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/34369917/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/34369917/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: IOS - uibutton的动态放置