菜鸟教程小白 发表于 2022-12-13 13:50:52

ios - UIBarButtonItem 错误地沿栏设置动画?


                                            <p><p>我正在尝试做一件简单的事情:在导航栏的右侧添加一个 UIBarButtonItem。看起来很简单:</p>

<pre><code>UIBarButtonItem *button = [ initWithTitle:@&#34;Button&#34; style:UIBarButtonItemStylePlain target:self action:@selector(buttonTapped)];
;
</code></pre>

<p>但是当调用该方法时,发生了一件奇怪的事情:<strong>按钮从栏的左侧一直滑动。</strong></p>

<p>右栏按钮不仅仅出现在右侧,而是出现在最左侧,快速滑过整个导航栏(在中间的标题下方),然后在右侧位置减速停止。从那时起,它完全按预期工作。</p>

<p>听起来很简单,但我无法让右栏按钮停止像这样滑入。我尝试将 <code>animated:NO</code> 和 <code>animated:YES</code> 添加到 <code>setRightBarButtonItem:</code> 方法中,但均无效。无论左侧是否有 UIBarButtonItem,都会发生这种情况。我试过使用 <code>setRightBarButtonItems:@</code>,但幻灯片动画没有改变。</p>

<p>有谁知道如何在导航栏的右侧添加一个简单的 UIBarButtonItem 而不会从侧面滑入?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您以某种方式将 <code>setRightBarButtonItem</code> 嵌入到某种动画中。<br/>
如果您<em>非常</em>仔细观察,您会注意到它实际上并不是按照说法从左到右,而是从 (0, 0)(大小为 (0, 0))到其最终静止位置和尺寸。 <code>navigationItem</code> 在动画中被劫持。</p>

<p>为了说服自己,你可以运行这个简单的代码片段:</p>

<pre class="lang-swift prettyprint-override"><code>func buttonTapped() {
    UIView.animateWithDuration(5) { () -&gt; Void in
      let button = UIBarButtonItem(title: &#34;Button&#34;,
                                     style: .Plain,
                                    target: self,
                                    action: &#34;buttonTapped&#34;)
      self.navigationItem .setRightBarButtonItem(button, animated: false)
    }
}
</code></pre>

<hr/>

<p><strong>演示</strong></p>

<p> <a href="/image/5L3Wm.gif" rel="noreferrer noopener nofollow"><img src="/image/5L3Wm.gif" alt="Animation demonstation"/></a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIBarButtonItem 错误地沿栏设置动画?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35025416/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35025416/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIBarButtonItem 错误地沿栏设置动画?