我想在特定操作发生后在代码中打开一个Popover(例如,点按一个按钮)。
以下代码让我打开一个带有NavigationBar的Popover,但它看起来不像我用IB做的那个。 (IB方法是在UINavigationController中嵌入UIViewController并定义两个按钮取消并保存)。我唯一想要的是在Popover上放置这两个按钮。我没有使用任何导航功能!
var cell = grid.VisibleCellAtCoordinate(coordinate) as SDataGridTextCell;
var viewController = new UIViewController();
var navBar = new UINavigationBar(new RectangleF(0, 0, viewController.View.Bounds.Width, 50))
{
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
BarStyle = UIBarStyle.Black,
Translucent = true,
Items = new[]
{
new UINavigationItem("test")
{
LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Cancel, (s, e) => { _popoverController.Dismiss(true); }),
RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Save, (s, e) => { _popoverController.Dismiss(true); })
}
}
};
var textField = new UITextField(new RectangleF(0, 50, viewController.View.Bounds.Width, viewController.View.Bounds.Height)) { Placeholder = "Mein Text...", BackgroundColor = UIColor.White };
textField.BecomeFirstResponder();
viewController.View.Add(navBar);
viewController.View.Add(textField);
viewController.View.SubviewAdded(textField);
_popoverController = new UIPopoverController(viewController);
_popoverController.SetPopoverContentSize(new SizeF(250, 200), false);
_popoverController.PresentFromRect(cell.Bounds, cell, UIPopoverArrowDirection.Any, false);
这些按钮看起来像这个图像(丑陋):
此图像显示了在IB中创建时的外观:
答案 0 :(得分:1)
这是在popover中使用独立导航栏的一个警告。据我所知,将视图控制器嵌入导航控制器是避免它的唯一方法。
答案 1 :(得分:0)
将内容视图(UIViewController)正确嵌入到 UINavigationController 有一些问题,因为我试图每次直接将导航栏项(按钮)添加到 UINavigationController ' s NavigationItem 属性。但这必须通过内容视图控制器来完成......
var contentViewController = new UIViewController();
var textField = new UITextField(contentViewController.View.Bounds)
{
BackgroundColor = UIColor.White,
AutoresizingMask = ~UIViewAutoresizing.None,
Placeholder = "My Text..."
};
contentViewController.View.Add(textField);
var navigationController = new UINavigationController(contentViewController);
contentViewController.Title = "Popover";
contentViewController.NavigationItem.LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Cancel, (s, e) => { _popoverController.Dismiss(false); });
contentViewController.NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Save, (s, e) => { _popoverController.Dismiss(false); });
_popoverController = new UIPopoverController(navigationController);
_popoverController.SetPopoverContentSize(new SizeF(300, 300), false);
_popoverController.PresentFromRect(button1.Bounds, button1, UIPopoverArrowDirection.Any, false);