菜鸟教程小白 发表于 2022-12-11 19:44:13

ios - NavigationItem 的 titleView 动画不正确


                                            <p><p>我已经查看了所有我能找到的类似问题,但我还没有找到解决这个问题的方法。 </p>

<p>我在 <code>viewDidLoad</code> 中像这样设置 <code>titleView</code> 属性:</p>

<pre><code>self.navigationItem.titleView = label
</code></pre>

<p> ViewController 是导航堆栈的一部分。当您点击前一个 ViewController 中的一行时,它会将这个 Controller 插入堆栈。完全正常的 UINavigationController 的东西。 </p>

<p>当这个 ViewController 开始动画时,标 checkout 现在原点(左上角),然后一直停留在那里,直到 ViewController 完成动画,然后它跳转(没有动画)到导航中间的正确位置酒吧。 </p>

<p>点击后退按钮后,标题 View 会正确显示动画,就像普通标题一样。 </p>

<p>这是<code>viewDidLoad</code>中的代码:</p>

<pre><code>let label = UILabel(frame: CGRect.zero)
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .red
label.text = &#34;test&#34;
label.sizeToFit()
self.navigationItem.titleView = label
</code></pre>

<p>我尝试过的事情:</p>

<ul>
<li><p>指定一个框架:没有变化。另外我不想指定一个框架。我不想对导航栏的高度做出假设。 </p></li>
<li><p>将其移至 <code>viewWillAppear</code>:没有变化。</p></li>
<li><p>将其移至 <code>viewDidAppear</code>:更好但仍然不正确。在动画完成之前,标签根本不会出现,然后它会出现在它应该出现的地方,没有动画它只是出现。正确的行为是像正常标题一样从右到左进行动画处理。 </p></li>
</ul>

<p>这很容易通过 Xcode 中的 Master-Detail 项目模板重现。如果你想尝试一下,只需将上面的代码添加到 <code>DetailViewController.swift</code> 中的 <code>configureView()</code> 顶部。在该模板中,导航项的标题被硬编码到 Storyboard 中。您可以通过搜索“详细信息”轻松删除它。单击显示 <code>Navigation Item: Title = "Detail"</code> 的结果,然后从 InspectorPane 中删除该字符串。 </p>

<hr/>

<p><strong>更新</strong></p>

<p>@synndicate 的建议确实适用于上面的 <code>UILabel</code> 示例。但我真正的问题是 <code>UIStackView</code>。当我按照@synndicate 建议的方法使用堆栈 View 时,我遇到了另一个动画问题。这次标题 View 开始滑入,但动画一直到原点。动画完成后,它会捕捉到它应该在的位置。</p>

<p>这是 <code>prepare(for:sender:)</code> 中的代码,正如@synndicate 所建议的...</p>

<pre><code>let label = UILabel(frame: CGRect.zero)
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .red
label.text = &#34;test&#34;
let stackView = UIStackView(frame: CGRect.zero)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubview(label)
stackView.sizeToFit()
controller.navigationItem.titleView = stackView
</code></pre>

<p>还有什么建议吗? </p>

<p>另外,这是 Xcode 8 和 iOS 10。</p>

<p><strong>更新 2</strong></p>

<p>我发现对于导航层次结构的根是 <code>UINavigationController</code>viewDidLoad</code> 的 ViewController ,上面的堆栈 View 代码完美地动画(就像普通标题一样)</code>。当导航层次结构的根是 <code>UISplitViewController</code> 时会出现问题。</p>

<p>所以我想我可以将我的问题更新为这个......</p>

<p>如何配置 <code>UIStackView</code> 将在 <code>navigationItem</code> 的 <code>titleView</code> 属性上设置,其中导航层次结构的根是<code>UISplitViewController</code>?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在将 ViewController 压入堆栈之前,您能否将代码移动到? </p>

<p>例如Master-Detail项目的<code>MasterViewController.swift</code>中</p>

<pre><code>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == &#34;showDetail&#34; {
            if let indexPath = tableView.indexPathForSelectedRow {                  

                let object = objects as! NSDate
                let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
                controller.navigationItem.leftItemsSupplementBackButton = true

                let label = UILabel()
                label.textAlignment = .center
                label.backgroundColor = .red
                label.text = &#34;test&#34;
                label.sizeToFit()

                controller.navigationItem.titleView = label

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