导航栏和导航按钮中的单独标题
嘿,我在 UINavigationController
内部有一堆 UIViewController
。通常,标题(或导航项的标题)决定导航栏中显示的标题(显示在顶部)和所有导航按钮,例如导航栏本身的后退按钮。
现在,我计划在导航栏的标题中添加更多信息,同时仍然保持这些按钮标签简短明了(例如视图的标题是“<客户名称>概述”,而按钮应该只显示“概述”)。
我目前正在尝试通过更改 ViewWillAppear
和 ViewWillDisappear
上的 NavigationItem 标题来实现此目的。这效果很好,但人们可以看到文本发生变化的时刻(可能是由于动画)。我也尝试了 ViewDidAppear
和 ViewDidDisappear
的不同组合,但仅使用 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 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,我认为有一个现成的解决方案:
在视图控制器的导航项上设置 backBarButtonItem 。
If I understand you correctly, I think there's a ready-made for this:
Set backBarButtonItem on your view controller's navigation item.