当ToolbarItem
的命令被设置为IOS的Xamarin.Forms
时,我正在努力如何从ToolbarItem
复制下拉式Secondary
,以使其看起来像Android一样。
这里有一些图片可以更好地解释我在寻找什么:
它在Android上如何运作:
ToolbarItem toolbarItem = new ToolbarItem()
{
Text = "ToolbarItem",
Order = ToolbarItemOrder.Secondary
};
图像显示“更多”图标
显示“更多”图标的图像已展开以显示更多工具栏项目
在iOS中将Order
设置为Secondary
时,工具栏上没有默认的“更多”图标。相反,会发生导航栏下方的栏,其中包含所有工具栏项目 - 我不希望我的应用程序使用这些项目。
这是在IOS之前如何实现它的一个例子:
我从我的一个实现此效果的应用程序中截取的屏幕截图
在原生iOS中,您可以使用UIPopoverController来实现您的效果。但请注意,此控件只能在iPad中使用。
由于您使用的是Xamarin.Forms,我们可以在iOS平台上创建自定义渲染器来实现此目的。
首先,创建一个页面渲染器来显示UIPopoverController
。我们可以根据您的请求从UIBarButtonItem或UIView中显示它。在这里我使用UIBarButtonItem:
//I defined the navigateItem in the method ViewWillAppear
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
rightItem = new UIBarButtonItem("More", UIBarButtonItemStyle.Plain, (sender, args) =>
{
UIPopoverController popView = new UIPopoverController(new ContentViewController());
popView.PopoverContentSize = new CGSize(200, 300);
popView.PresentFromBarButtonItem(rightItem, UIPopoverArrowDirection.Any, true);
});
NavigationController.TopViewController.NavigationItem.SetRightBarButtonItem(leftItem, true);
}
其次,在UIPopoverController中构造内容ViewController(就像android中的辅助列表一样):
public class ContentViewController : UIViewController
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
UITableView tableView = new UITableView(new CGRect(0, 0, 200, 300));
tableView.Source = new MyTableViewSource();
View.AddSubview(tableView);
}
}
public class MyTableViewSource : UITableViewSource
{
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(new NSString("Cell"));
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Default, new NSString("Cell"));
}
cell.TextLabel.Text = "Item" + indexPath.Row;
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return 10;
}
}
最后我们可以通过调用PresentFromBarButtonItem
在屏幕上显示它。