在NavigationBar和导航按钮中分隔标题 [英] Separate title in NavigationBar and navigation buttons

查看:122
本文介绍了在NavigationBar和导航按钮中分隔标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我在 UINavigationController 里面有一堆 UIViewController 。通常标题(或NavigationItem的标题)决定导航栏中显示的标题(显示在顶部)所有导航按钮,例如导航中的后退按钮bar本身。

Hey, I have a stack of UIViewControllers inside of a UINavigationController. Usually the title (or the NavigationItem‘s title) decides about both the title that is displayed in the NavigationBar (displayed at the top) and all the navigation buttons, for example the back-buttons in the navigation bar itself.

现在我计划在NavigationBar的标题中添加更多信息,同时仍然保持那些按钮标签简洁明了(例如视图的标题是<客户名称>概述,而按钮应该只显示概述。

Now I plan to put a bit more information into the NavigationBar‘s title, while still keeping those button labels short and concise (for example the view‘s title is "<Customer name> Overview", while the buttons should just show "Overview").

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

I am currently trying to achieve this by changing the NavigationItem‘s title on ViewWillAppear and ViewWillDisappear. This works well, but one can see the moments where the text changes (probably due to the animation). I‘ve tried different combinations with ViewDidAppear and ViewDidDisappear as well, but the effect was the best with just the Will methods. An example code for this is shown below (the Example class is pushed to a UINavigationController).

是否有更好的方法来实现这一目标?也许只需更改按钮标题或只是直接更改导航的标题?或者我可以阻止标准机制将标题复制到所有其他实例?

Is there a better way to achive this? Maybe with simply changing the button titles only or with just directly changing the navigation‘s title? Or can I maybe prevent the standard mechanisms from copying the title to all other instances?

public class Example : UIViewController
{
    private int depth;

    public Example ( int depth = 0 )
    {
        this.depth = depth;
        this.Title = "Title";
    }

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

        UIButton btn = new UIButton( new RectangleF( 100, 100, 300, 50 ) );
        btn.SetTitle( "Duplicate me!", 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( "Title / {0}", depth );
    }

    public override void ViewWillDisappear ( bool animated )
    {
        base.ViewWillDisappear( animated );
        this.NavigationItem.Title = "Title";
    }
}


推荐答案

如果我理解正确,我认为有一个现成的:

If I understand you correctly, I think there's a ready-made for this:

在视图控制器的导航项上设置backBarButtonItem。

Set backBarButtonItem on your view controller's navigation item.

这篇关于在NavigationBar和导航按钮中分隔标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆