NavigationBar 和导航按钮中的单独标题 [英] Separate title in NavigationBar and navigation buttons
问题描述
嘿,我在 UINavigationController
中有一堆 UIViewController
.通常,标题(或 NavigationItem 的标题)决定了显示在 NavigationBar(显示在顶部)中的标题和所有导航按钮,例如导航中的后退按钮酒吧本身.
Hey, I have a stack of UIViewController
s 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").
我目前正在尝试通过更改 ViewWillAppear
和 ViewWillDisappear
上的 NavigationItem 标题来实现这一点.这很有效,但可以看到文本发生变化的时刻(可能是由于动画).我也尝试过使用 ViewDidAppear
和 ViewDidDisappear
的不同组合,但仅使用 Will
方法效果最佳.下面显示了一个示例代码(Example
类被推送到 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).
有没有更好的方法来实现这一目标?也许只需更改按钮标题only 或直接更改导航的标题?或者我可以阻止标准机制将标题复制到所有其他实例吗?
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屋!