0

I am trying to display a modal popover using Xamarin.iOS and MVVMCross.

Here is my view:

[MvxModalPresentation(
        WrapInNavigationController = true,
        ModalPresentationStyle = UIModalPresentationStyle.Popover,
        PreferredContentSize = new CGSize(100, 100)
)]
public class Test2View : BaseViewController<TestView2Model>
{
    //other code...
}

This doesn't compile. The PreferredContentSize attribute has an error.

'PreferredContentSize' is not a valid named attribute argument because it has type 'CoreGraphics.CGSize', which is not a valid attribute parameter type

As a follow up question, is there a way to have a modal popover of a specific size on an iPhone?

Hackmodford
  • 3,901
  • 4
  • 35
  • 78

1 Answers1

0

The reason it doesn't compile is explained here:

This is a CLR restriction. Only primitive constants or arrays of primitives can be used as attribute parameters. The reason why is that an attribute must be encoded entirely in metadata. This is different than a method body which is coded in IL. Using MetaData only severely restricts the scope of values that can be used. In the current version of the CLR, metadata values are limited to primitives, null, types and arrays of primitives (may have missed a minor one).

What you could do is use a different UIModalPresentationStyle which suits your use case better. You can find more info about each style here (see the section 'Presentations').

If none suits your needs and you still want a specific size for your pop-up you can try this:

[MvxModalPresentation(WrapInNavigationController = true, ModalPresentationStyle = UIModalPresentationStyle.Popover)]
public class Test2View : BaseViewController<TestView2Model>
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        // Set your specific size
        PreferredContentSize = new CGSize(300, 300);
    }
}
JKL
  • 978
  • 7
  • 21
  • I just tried this. It compiles but there is no popover on my iPhone simulator. Note: iPhone and not iPad simulator. – Hackmodford Dec 21 '19 at 21:32
  • In the documentation of `UIModalPresentationStyle` which I provided in my answer you can find that in a horizontally compact environment `UIModalPresentationStyle.Popover` behaves the same as `UIModalPresentationStyle.fullScreen`. Just play around with some of the other options and see which suits your needs... – JKL Dec 22 '19 at 12:08
  • Yeah. I think I need to figure out how I did it in the native iOS environment. I'm going to mark this answer as correct. – Hackmodford Jan 07 '20 at 21:25