导航栏和导航按钮中的单独标题

发布于 2024-10-23 20:50:34 字数 1571 浏览 5 评论 0原文

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

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

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

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

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

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.

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”).

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风几经秋 2024-10-30 20:50:34

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

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

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

Set backBarButtonItem on your view controller's navigation item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文