0

I want to open a Popover in code after a specific action happens (e.g. tap on a button).

The following Code let me open a Popover with a NavigationBar but it don't looks like the one I've done with IB. (IB approach was embedding a UIViewController in a UINavigationController and defining the the two buttons cancel and save). The only thing I want is having these two buttons on top of the Popover. I don't use any navigation functionality!

        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);

These buttons looks like this image (ugly):
Ugly - From Code

And this image shows how it's look like when created in IB:
Nice - From IB

dannyyy
  • 1,784
  • 2
  • 19
  • 43

2 Answers2

1

That's one of the caveats of using a standalone navigation bar in a popover. Embedding your view controller in a navigation controller is, as far as I know, the only way to avoid it.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • Thank you, I've added my new solution based on your answer! Now is the appearance the same as designed in IB. – dannyyy Aug 07 '13 at 07:05
0

Had some problem embedding the content view (UIViewController) correctly to the UINavigationController because I tried to add the navigation bar items (buttons) each time directly to the UINavigationController's NavigationItem property. But this must be done through the content view controller...

        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);
dannyyy
  • 1,784
  • 2
  • 19
  • 43