How to do a transparent modal in Xamarin.iOS
- When presenting a UIViewController the ModalPresentationStyle is set to UIModalPresentationStyle.OverCurrentContext
partial void ShowDetailAction(UIButton sender)
{
var detailViewController = Storyboard.InstantiateViewController("DetailView");
//This is what makes presenting over another VC possible.
detailViewController.ModalPresentationStyle = UIModalPresentationStyle.OverCurrentContext;
PresentViewController(detailViewController, true, null);
}
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621355-modalpresentationstyle
- Set the background of the modal view to be semi transparent / Set the View Opaque property to false
public override void ViewDidLoad()
{
base.ViewDidLoad();
View.BackgroundColor = UIColor.Black.ColorWithAlpha(0.5f);
View.Opaque = false;
}- Include another non-transparent view, or embedded a container UIViewController to hold the focused content https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html
Keep in mind that adds a little complexity and may or may not be needed. It is not shown in this example.
- Add a tap gesture recognizer to close the view.
- Go to the toolbox select Tap Gesture
- Drag it to the view
- Add an event handler
- Close the ModalViewController using the DismissViewController method
partial void TapAction(UITapGestureRecognizer sender)
{
DismissViewController(true, null);
}