菜鸟教程小白 发表于 2022-12-12 16:05:28

ios - NavigationBar 和导航按钮中的单独标题


                                            <p><p>嘿,我在 <code>UINavigationController</code> 中有一堆 <code>UIViewController</code>。通常标题(或 NavigationItem 的标题)决定了 NavigationBar 中显示的标题(显示在顶部)<em>和</em>所有导航按钮,例如导航中的后退按钮酒吧本身。</p>

<p>现在我计划在 NavigationBar 的标题中添加更多信息,同时仍然保持这些按钮标签简短明了(例如 View 的标题是“<客户名称>概览”,而按钮应该只是显示“概览”)。</p>

<p>我目前正在尝试通过在 <code>ViewWillAppear</code> 和 <code>ViewWillDisappear</code> 上更改 NavigationItem 的标题来实现此目的。这很好用,但可以看到文本发生变化的时刻(可能是由于动画)。我也尝试了 <code>ViewDidAppear</code> 和 <code>ViewDidDisappear</code> 的不同组合,但仅使用 <code>Will</code> 方法效果最好。下面显示了一个示例代码(<code>Example</code> 类被推送到 UINavigationController)。</p>

<p>有没有更好的方法来实现这一点?也许只需更改按钮标题 <em>only</em> 或直接更改导航的标题?或者我可以阻止标准机制将标题复制到所有其他实例吗?</p>

<pre><code>public class Example : UIViewController
{
    private int depth;

    public Example ( int depth = 0 )
    {
      this.depth = depth;
      this.Title = &#34;Title&#34;;
    }

    public override void ViewDidLoad ()
    {
      base.ViewDidLoad();

      UIButton btn = new UIButton( new RectangleF( 100, 100, 300, 50 ) );
      btn.SetTitle( &#34;Duplicate me!&#34;, UIControlState.Normal );
      btn.TouchDown += delegate(object sender, EventArgs e) {
            NavigationController.PushViewController( new Example( depth + 1 ), true );
      };
      View.Add( btn );
    }

    public override void ViewWillAppear ( bool animated )
    {
      base.ViewWillAppear( animated );
      this.NavigationItem.Title = String.Format( &#34;Title / {0}&#34;, depth );
    }

    public override void ViewWillDisappear ( bool animated )
    {
      base.ViewWillDisappear( animated );
      this.NavigationItem.Title = &#34;Title&#34;;
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果我理解正确的话,我认为有一个现成的:</p>

<p>在 ViewController 的导航项上设置 backBarButtonItem。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - NavigationBar 和导航按钮中的单独标题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/5338547/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/5338547/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - NavigationBar 和导航按钮中的单独标题