开发中基本上每个界面都会有导航栏,来控制你每个界面从哪来到哪去。
通常的情况都是导航栏左侧一个返回按钮,中间文字展示这个界面是干嘛的。
偶尔也会出现右侧一个图标或者文字,来增加一些功能。本篇介绍一下几个简单位置按钮设置。
1.设置中间文字。
一般都放在ViewController的构造里面,下同。
UILabel titleUILabel = new UILabel(new CGRect(0, 0, 100, 35));
titleUILabel.Text = "中间文字";
NavigationItem.TitleView = titleUILabel;
titleUILabel.TextAlignment = UITextAlignment.Center;
titleUILabel.TextColor = UIColor.White;
2.设置右侧按钮(图片)。
// 右侧成长记录按钮
var rightImg = UIImage.FromBundle("图片资源路径");
UIBarButtonItem rightBtn = new UIBarButtonItem(rightImg, UIBarButtonItemStyle.Plain
, (sender, e) =>
{
// 点击事件
});
NavigationItem.SetRightBarButtonItem(rightBtn, true);
3.左侧返回按钮(文字代替),右侧按钮(文字)
// 标题右侧保存按钮
UIBarButtonItem rightItem = new UIBarButtonItem("保存", UIBarButtonItemStyle.Plain, (sender, e) =>
{
// 右侧文字点击
});
NavigationItem.SetRightBarButtonItem(rightItem, true);
// 取消按钮
UIBarButtonItem leftItem = new UIBarButtonItem("取消", UIBarButtonItemStyle.Plain
, (sender, e) =>
{
// 左侧文字点击
});
NavigationItem.SetLeftBarButtonItem(leftItem, true);